问题描述
我正在寻找当我允许在另一个类的头文件中声明一个类的定义:
我允许为一个类?
放置自己在编译器的位置:当你转发声明一个类型,所有编译器知道的是这种类型存在;它不知道它的大小,成员或方法。这就是为什么它被称为不完整类型。因此,您不能使用类型来声明成员或基类,因为编译器将需要知道类型的布局。
假设以下前向声明。
class X;
以下是您可以做什么和不能做什么。
您可以使用不完整类型执行的操作:
-
或对不完整类型的引用:
class Foo {
X * pt;
X& pt;
};
-
声明接受/返回不完整类型的函数或方法:
void f1(X);
X f2();
-
定义函数或接受/返回指针/ (但不使用其成员):
void f3(X *,X&){}
X& f4(){}
X * f5(){}
不完整类型无法处理的:
-
作为基类
class Foo:X {} //编译器错误!
-
使用它来声明成员:
class Foo {
X m; //编译错误!
};
-
void f1(X x){} //编译器错误!
X f2(){} //编译器错误!
-
使用其方法或字段,实际上试图取消引用不完整类型的变量
class Foo {
X * m;
void method()
{
m-> someMethod(); //编译错误!
int i = m-> someField; //编译错误!
}
};
对于模板,没有绝对规则:是否可以使用不完整的类型作为模板参数取决于在模板中使用的类型。
例如, std :: vector< T>
需要其参数是一个完整的类型,而 boost :: container :: vector& T>
不会。有时,只有使用某些成员函数才需要完整类型; 。
一个记录良好的模板应在其文档中指明所有要求的参数,包括他们是否需要是完整的类型。
I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:
Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?
解决方案 Put yourself in the compiler's position: when you forward declare a type, all the compiler knows is that this type exists; it knows nothing about its size, members, or methods. This is why it's called an incomplete type. Therefore, you cannot use the type to declare a member, or a base class, since the compiler would need to know the layout of the type.
Assuming the following forward declaration.
class X;
Here's what you can and cannot do.
What you can do with an incomplete type:
Declare a member to be a pointer or a reference to the incomplete type:
class Foo {
X *pt;
X &pt;
};
Declare functions or methods which accept/return incomplete types:
void f1(X);
X f2();
Define functions or methods which accept/return pointers/references to the incomplete type (but without using its members):
void f3(X*, X&) {}
X& f4() {}
X* f5() {}
What you cannot do with an incomplete type:
Use it as a base class
class Foo : X {} // compiler error!
Use it to declare a member:
class Foo {
X m; // compiler error!
};
Define functions or methods using this type
void f1(X x) {} // compiler error!
X f2() {} // compiler error!
Use its methods or fields, in fact trying to dereference a variable with incomplete type
class Foo {
X *m;
void method()
{
m->someMethod(); // compiler error!
int i = m->someField; // compiler error!
}
};
When it comes to templates, there is no absolute rule: whether you can use an incomplete type as a template parameter is dependent on the way the type is used in the template.
For instance, std::vector<T>
requires its parameter to be a complete type, while boost::container::vector<T>
does not. Sometimes, a complete type is required only if you use certain member functions; this is the case for std::unique_ptr<T>
, for example.
A well-documented template should indicate in its documentation all the requirements of its parameters, including whether they need to be complete types or not.
这篇关于我什么时候可以使用转发声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!