Question

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 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?

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top