C++ 如何转发声明内部类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1021793/
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
How do I forward declare an inner class?
提问by bradtgmurray
Possible Duplicate:
Forward declaration of nested types/classes in C++
可能的重复:
C++ 中嵌套类型/类的前向声明
I have a class like so...
我有一个这样的班...
class Container {
public:
class Iterator {
...
};
...
};
Elsewhere, I want to pass a Container::Iterator by reference, but I don't want to include the header file. If I try to forward declare the class, I get compile errors.
在其他地方,我想通过引用传递 Container::Iterator,但我不想包含头文件。如果我尝试转发声明该类,则会出现编译错误。
class Container::Iterator;
class Foo {
void Read(Container::Iterator& it);
};
Compiling the above code gives...
编译上面的代码给出...
test.h:3: error: ‘Iterator' in class ‘Container' does not name a type
test.h:5: error: variable or field ‘Foo' declared void
test.h:5: error: incomplete type ‘Container' used in nested name specifier
test.h:5: error: ‘it' was not declared in this scope
How can I forward declare this class so I don't have to include the header file that declares the Iterator class?
如何转发声明此类,以便不必包含声明 Iterator 类的头文件?
采纳答案by JaredPar
This is simply not possible. You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.
这根本不可能。您不能在容器外转发声明嵌套结构。您只能在容器内转发声明它。
You'll need to do one of the following
您需要执行以下操作之一
- Make the class non-nested
- Change your declaration order so that the nested class is fully defined first
- Create a common base class that can be both used in the function and implemented by the nested class.
- 使类非嵌套
- 更改您的声明顺序,以便首先完全定义嵌套类
- 创建一个公共基类,既可以在函数中使用,也可以由嵌套类实现。
回答by Todd Gardner
I don't believe forward declaring inner class of on an incomplete class works (because without the class definition, there is no way of knowing if there actually isan inner class). So you'll have to include the definition of Container, with a forward declared inner class:
我不相信向前不完整的类作品声明内部类的(因为没有类定义,没有办法知道是否有实际的方式是一个内部类)。因此,您必须包含 Container 的定义,以及一个前向声明的内部类:
class Container {
public:
class Iterator;
};
Then in a separate header, implement Container::Iterator:
然后在一个单独的头文件中,实现 Container::Iterator:
class Container::Iterator {
};
Then #include only the container header (or not worry about forward declaring and just include both)
然后#include 只包含容器头(或者不用担心前向声明,只包含两者)
回答by Faisal Vali
I know of no way to do exactly what you want, but here is a workaround, if you are willing to use templates:
我知道没有办法完全按照您的意愿去做,但如果您愿意使用模板,这里有一个解决方法:
// Foo.h
struct Foo
{
export template<class T> void Read(T it);
};
// Foo.cpp
#include "Foo.h"
#include "Container.h"
/*
struct Container
{
struct Inner { };
};
*/
export template<>
void Foo::Read<Container::Inner>(Container::Inner& it)
{
}
#include "Foo.h"
int main()
{
Foo f;
Container::Inner i;
f.Read(i); // ok
f.Read(3); // error
}
Hopefully, this idiom might be of some use to you (and hopefully your compiler is EDG-based and implements export ;) ).
希望这个习语可能对您有用(希望您的编译器基于 EDG 并实现导出 ;))。