一个简单的例子:
void foo(class Bar * bar) {
foo2(bar);
}
void foo2(Bar * bar) {
bar->a++;
}
foo2中使用的指针是类Bar的标准指针。 OK - foo中使用的指针也是指向类Bar的指针。但是在这个地方一定不能知道班级吧。
我找不到foo参数的正确命名。它是一个指向匿名类的指针? class Bar *bar
的正确名称是什么?
你需要的是forward declaration
。
你可以读一下in the documentation或in a different post。
从文档:
Declares a class type which will be defined later in this scope. Until the definition appears, this class name has incomplete type. This allows classes that refer to each other.
我会称这是一个内联前向类声明,但我不认为这有一个特殊的名称。
它只是告诉我们有一个类Bar
所以它可以在这个范围内使用。
你不能用这个来调用Bar
的成员。所有它说是有,它没有说它是什么。 Bar
是这样的不完整类型。