问题描述
我知道在转发声明之后,例如
I know that after a forward declaration such as
class Foo;
我可以声明一个变量为 Foo *
或 Foo&
,但不是 Foo
。
我有一个模板类和实例化
If I have a templated class and instantiation
template<class T>
class Bar {
public:
T baz;
};
...
Bar<Foo> v;
上述规则将如何应用? Foo必须在定义类 Bar
的时候完全声明(而不是仅向前声明),或者在 v
是否已声明?或者也许只需要在源文件中的任何地方使用 Bar
,所有其他的不是?
how would the above rule apply? Would Foo have to have been fully declared (as opposed to only forward declared) at the point the class Bar
is defined, or at the point v
is declared? Or maybe it only has to be at one point that Bar<Foo>
is used anywhere in the source files, and all the others it is not? Something else?
感谢
推荐答案
Bar< Foo>
需要完整的 Foo
定义,因为它包含 Foo
。在这种情况下,您不能转发声明。在这方面, Bar
等效于
Bar<Foo>
requires the full Foo
definition because it contains a data member of type Foo
. You could not forward declare in this case. In this respect, Bar<Foo>
is the equivalent of
class BarFoo
{
public:
Foo baz;
};
这篇关于使用类作为具有前向声明的模板类的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!