问题描述
我知道不能在 C++ 中前向声明 typedef,但我想知道以下问题的最佳解决方案是什么.我有一个像这样声明 MyClass
的头文件:
I know a typedef cannot be forward declared in C++, but I wonder what may be the best solution for the following problem. I have a header file which declares MyClass
like this:
#include <TemplateClassHeaders>
struct MyStruct
{
// ...
}
typedef Really::Long::TemplateClassName<MyStruct> MyClass;
MyClass
用在很多地方,但主要是作为通过 Qt 信号槽机制传递的指针.只需要一个前向声明,但由于 MyClass
是一个无法工作的 typedef.有没有办法避免将 myclass.h 添加到使用指向 MyClass
的指针的每个标头中?
MyClass
is used in many places, but mostly just as a pointer being passed through Qt signal-slot mechanism. A forward declaration would be all that's needed, but since MyClass
is a typedef that will not work. Is there a way to avoid adding myclass.h to every header which uses a pointer to MyClass
?
推荐答案
您不能向前声明 typedef.但是如果你向前声明它所依赖的类,TemplateClassName
和 MyStruct
,你应该能够定义 MyClass
.
You cannot forward-declare a typedef. But if you forward-declare the classes it relies on, both TemplateClassName
and MyStruct
, you should be able to define MyClass
.
namespace Really {
namespace Long {
template <typename>
class TemplateClassName;
}
}
class MyStruct;
typedef Really::Long::TemplateClassName<MyStruct> MyClass;
MyClass *p = 0;
这篇关于前向声明模板类的 typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!