Forward-declare typedef

I have a large header file (~ 10,000 lines) that is automatically generated by the script / program from my control.

In order not to include this file in the declaration of my class, I forward the declaration to several types that I need:

- myclass.h

namespace bl { class TypeA; class TypeB; } // Other stuff and myclass definition... 

Now it turns out that TypeA and TypeB are not class names, but instead are defined inside an automatically generated file as:

 typedef SomeUnspecifiedClassName TypeA; typedef AnotherUnspecifiedClassName TypeB; 

Where by SomeUnspecifiedClassName I mean that I cannot forward-declare this type-name because it can change under different circumstances.

How can I send typedef declaration? (Unable to use C ++ 11)

+4
source share
3 answers

Just - you can’t. However, if you publish your specific situations, there may be some workarounds for what you want to do.

+6
source

You can write a script that extracts ...UnspecifedClassName from the typedef lines in your autogenerated source file. Then this script will be the basis of your own automatically generated header file, which will pass declarations to these classes, as well as your typedef instructions to them. Your file myclass.h can then #include header file.

+4
source

One of the relatively decent solutions that I found useful sometimes is to create a trivial wrapper class:

Put in the header file:

 class ClassA; // now use pointers and references to ClassA at will 

Location in source file:

 #include <NastyThirdPartyHeader> class ClassA: public TypeA { public: ClassA(TypeA const &x): TypeA(x) {} ClassA &operator=(TypeA const &x) { TypeA::operator=(x); return *this; } }; 

Depending on your use case, this may be all you need.

+1
source

All Articles