简体   繁体   English

声明一个C ++类,而不在当前的转换单元中定义它

[英]Declare a C++ class without defining it in the current translation unit

It is possible to declare a class without defining it (forward declaration) as long as it is defined later on within the translation unit. 只要稍后在翻译单元中定义,就可以声明一个类而不定义它(前向声明)。 In the case of functions, one can declare a function without defining it within the translation unit, and the linker will link it to its definition in a different translation unit. 在函数的情况下,可以声明函数而不在转换单元中定义它,并且链接器将在不同的转换单元中将其链接到其定义。 Is it possible to do the same with class declarations? 是否有可能对类声明做同样的事情?

(if this is not possible, is there any use to a forwardly declared class without a definition in the current TL, or is that always an error?) (如果这不可能,对于在当前TL中没有定义的前向声明的类有什么用处,还是总是出错?)

something like this, except this doesn't compile: 像这样的东西,除了这不编译:

mymain.cpp: mymain.cpp:

class myclass; // declare without defining

myclass::myclass();
void myclass::barf();

int main() {
  myclass *m = new myclass();
  m->barf();
  return 0;
}

myclass.cpp: myclass.cpp:

#include <iostream>

class myclass { // define the implementation
public:
    myclass();
    void barf();
};

myclass::myclass() { } //empty constructor
void myclass::barf() {
    std::cout << "barfing\n";
}

It is possible to forward-declare a class, but only pointers and references to forward-declared classes can be used. 可以向前声明一个类,但只能使用指向前向声明的类的指针和引用。 You can't use an actual object of a forward-declared class because the compiler doesn't know enough about it; 你不能使用前向声明类的实际对象,因为编译器对它不够了解; in particular it doesn't know how large the object is or what its members are. 特别是它不知道对象有多大或者成员是什么。 Trying to forward-declare member functions (as you have done) won't work because the syntax of the forward declaration doesn't allow you to specify whether the functions are virtual or non-virtual (or perhaps inherited from some base class). 尝试转发声明成员函数(正如您所做的那样)将无法工作,因为前向声明的语法不允许您指定函数是虚函数还是非虚函数(或者可能是从某个基类继承的)。

It is not often useful to forward-declare a class in a source file, but it can be useful in a header file. 在源文件中转发声明类通常不常用,但它在头文件中很有用。 In particular it's common to forward-declare a class in a library's public header file and use pointers to that type as opaque handles. 特别是在库的公共头文件中转发声明一个类并使用指向该类型的指针作为不透明句柄是很常见的。 The class definition remains private to the library but user code can pass around pointers to objects of that class without ever knowing what the class's implementation looks like. 类定义对库来说是私有的,但是用户代码可以传递指向该类对象的指针,而不知道类的实现是什么样的。 This works particularly well when the pointers are smart pointers. 当指针是智能指针时,这种方法特别有效。

You can, but only if you use exclusively pointers or references to that class. 您可以,但前提是您只使用指针或对该类的引用。 But you can't use code referring to that class' members (variables or methods). 但是你不能使用引用该类的成员(变量或方法)的代码。 You can only use it to declare pointer variables to that class. 您只能使用它来向该类声明指针变量。

I would suggest you create a myclass.h header file with myclass' full declaration, and include that header in mymain.cpp . 我建议你使用myclass的完整声明创建一个myclass.h头文件,并在mymain.cpp包含该头mymain.cpp

You can only do that through hacks . 你只能通过黑客来做到这一点。

The declare before use rule doesn't hold within a class ( see here ). 使用前声明规则不在一个类中( 参见此处 )。

Otherwise you can do that by declaring your function as a template function, whose template parameter is of myclass (in your example). 否则,您可以通过将函数声明为模板函数来实现,模板函数的模板参数是myclass (在您的示例中)。

The only non- hack way is to define the class (ie. by including the header file its defined in). 唯一的非破解方式是定义类(即通过包含其定义的头文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM