4

I have got a large header file (~10000 lines) which is auto-generated by a script/program out of my control.

In order to avoid to include this file in the declaration of my class, I forward declare the few types 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 are instead defined inside the auto-generated file as:

typedef SomeUnspecifiedClassName TypeA;
typedef AnotherUnspecifiedClassName TypeB;

Where by SomeUnspecifiedClassName I mean that I can not forward-declare this type-name because it may change under various circumstances.

How can I forward-declare a typedef? (Can't use c++11)

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
sbabbi
  • 11,070
  • 2
  • 29
  • 57
  • Can you use the class via references and pointers? http://stackoverflow.com/questions/553682/when-to-use-forward-declaration –  Apr 29 '13 at 20:45
  • The answer to your specific question in your last sentence is already answered in the first related question: http://stackoverflow.com/questions/804894/forward-declaration-of-a-typedef-in-c?rq=1 – jxh Apr 29 '13 at 20:46
  • Depending on where `TypeA` and `TypeB` are being used, you could consider refactoring `myclass` into a template. – jxh Apr 29 '13 at 20:53

3 Answers3

6

Simply - you can't. If you post your specific situations, though, there might be some workarounds to what you want to do.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
  • I worked around with some macros (a lot of, actually), by typedef-ing directly the "unspecified" types. – sbabbi Apr 29 '13 at 23:03
4

You can write a script that extracts the ...UnspecifedClassName from the typedef lines in your auto-generated source file. Then, this script would be the basis of your own auto-generated header file that would forward declare those classes, as well as your typedef statements to them. Your myclass.h file can then #include that header file.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

One relatively decent solution that I have found useful on occasion is to create a trivial wrapper class:

Place in a header file:

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

Place in a 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, that may be all you need.