C++ 类、函数的前向声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4341198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:10:36  来源:igfitidea点击:

Forward Declaration of Class, Function

c++visual-c++

提问by Mahesh

When forward declarations of functions work in a source file (.cpp), why would the same doesn't work for classes ?

当函数的前向声明在源文件 (.cpp) 中工作时,为什么同样不适用于类?

Thanks.

谢谢。

// main.cpp

void forwardDeclaredFunction() ; // This is correct 

class One ; // Why this would be wrong 

int One:: statVar = 10 ;

void
One :: anyAccess() {

 std::cout << "\n statVar:\t " << statVar ;
 std::cout << "\n classVar:\t" << classVar ;
}

class One {

 public:
  void anyAccess() ;
  static int statVar ;

 private:
  int  classVar ;

} ;


int main (int argc, char * const argv[]) {

 One *obj = new One ;

        return 0;
}

void forwardDeclaredFunction() {
}

回答by The Maniac

Forward declaration can work for classes too:

前向声明也适用于类:

class Foo;

class Bar {
public:
    Foo *myFoo; // This has to be a pointer, thanks for catching this!
};

class Foo {
public:
    int value;
};

The above code shows a forward declaration of the Foo class, using a variable of type Foo* in another class (Bar), then the actual definition of the Foo class. C++ doesn't care if you leave things unimplemented as long as you implement them before using its code. Defining pointers to objects of a certain type is not "using its code."

上面的代码显示了 Foo 类的前向声明,在另一个类 (Bar) 中使用了 Foo* 类型的变量,然后是 Foo 类的实际定义。只要您在使用其代码之前实现它们,C++ 并不关心您是否留下未实现的东西。定义指向某种类型对象的指针并不是“使用其代码”。

Quick, dirty reply but I hope it helps.

快速,肮脏的回复,但我希望它有所帮助。

Edit:Declaring a non-pointer variable of a class thats unimplemented will NOT compile as the replies stated out. Doing so is exactly what I meant by "using its code." In this case, the Foo constructor would be called whenever the Bar constructor is called, given that it has a member variable of type Foo. Since the compiler doesn't know that you plan on implementing Foo later on, it will throw an error. Sorry for my mistake ;).

编辑:声明未实现的类的非指针变量将不会像回复所述那样编译。这样做正是我所说的“使用其代码”的意思。在这种情况下,只要调用 Bar 构造函数,就会调用 Foo 构造函数,因为它有一个 Foo 类型的成员变量。由于编译器不知道您打算稍后实现 Foo,因此它会抛出错误。对不起,我错了 ;)。

回答by zwol

The forward declaration class One;allows you to refer to the class itselfbut not to any of its members. You have to put all definitions of class members after the full declaration of the class. (Or inside, of course.)

前向声明class One;允许您引用类本身,但不能引用其任何成员。您必须在类的完整声明之后放置类成员的所有定义。(当然,或者在里面。)

回答by Daniel A. White

Place your member declaration of your class before the member implementations.

将您的类的成员声明放在成员实现之前。

class One {

 public:
  void anyAccess() ;
  static int statVar ;

 private:
  int  classVar ;

} ;

int One:: statVar = 10 ;

void
One :: anyAccess() {

 std::cout << "\n statVar:\t " << statVar ;
 std::cout << "\n classVar:\t" << classVar ;
}

回答by Mark B

You're getting the error message on int One:: statVar = 10 ;NOT on the forward declaration, which is fine.

int One:: statVar = 10 ;在前向声明中收到关于NOT的错误消息,这很好。

The compiler needs to know the full definition of the class before you can define static members like that - a forward declaration is insufficient (it needs to be able to confirm that the type is correct from the class definition).

编译器需要知道类的完整定义,然后才能像这样定义静态成员 - 前向声明是不够的(它需要能够从类定义中确认类型是正确的)。

You'll need to move your static attribute definition below the class definition.

您需要将静态属性定义移动到类定义下方。

回答by cHao

The compiler reads stuff from beginning to end, and generates code as it goes. (Some compilers may not do this, but they should behave as if they did.) But before the class is defined, the compiler doesn't know that One::statVaror One::anyAccessshould exist, or whether the function is virtual, static, or what. It needs to know that stuff in order to generate code.

编译器从头到尾读取内容,并生成代码。(有些编译器可能不会这样做,但它们的行为应该像它们那样。)但是在定义类之前,编译器不知道该类One::statVarOne::anyAccess应该存在该类,也不知道该函数是虚拟的、静态的还是什么。它需要知道这些东西才能生成代码。

回答by cHao

when you create 2 class & one function can access data from on class to another class then it is a friend function

当您创建 2 个类并且一个函数可以从一个类访问数据到另一个类时,它就是一个友元函数

forword declaration is use to know which class in next

前言声明用于知道接下来的哪个类

class abc;

abc 级;

class xyz

类 xyz

{

{

data member;

数据成员;

public:

民众:

friend void getdata();

朋友 void getdata();

other member function

其他成员函数

}

}

class abc

abc 类

{

{

data member

数据成员

public:

民众:

friend void getdata();

朋友 void getdata();

}

}