前向声明模板类的 typedef [英] Forward declaring a typedef for a template class

查看:48
本文介绍了前向声明模板类的 typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道不能在 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.但是如果你向前声明它所依赖的类,TemplateClassNameMyStruct,你应该能够定义 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆