Forward declarations

From: Marcin Kalicinski (kalita_at_poczta.onet.pl)
Date: 05/11/04


Date: Tue, 11 May 2004 16:35:12 +0200

Hi All,

In some headers meant to work with both C and C++ the following is often
found:

typedef struct tagMyStruct { /*some members*/ } MyStruct;

Can I forward-declare MyStruct somehow?

Or more generally, can I forward declare a typedef or an enum? Also, is
there any reason why there should be difference between forward declarations
of classes and structs (i.e. why I need to explicitly write keyword 'struct'
or 'class' when forward declaring). In some cases I don't immediately know
what to write, because, for example, the type definition in original header
file is generated with some macro, such as this:

STDINTERFACE(IBlahBlah, IBaseBlahBlah) {
    /* Members */
}

Now I need to search for the definition of STDINTERFACE to see how it works.
Even if I find it, it still may silently change in some future version of
the header file, and cause my compilation to fail. Wouldn't allowing a
generic form of forward declaration, without introducing a new keyword:

typename IBlahBlah;

be beneficial to C++?

best regards,
Marcin



Relevant Pages

  • Re: static library problems
    ... Do not declare variables in header files. ... variable itself must be declared in a .cpp file. ... >If i want to use this string in other modules, i just need include the header, i need not link this static lib. ...
    (microsoft.public.vc.mfc)
  • Re: Variables and scope ? !!!!
    ... without seeing the project but i no professional vba programmer but would ... > global -- available to all code in the application, declared in the header ... > a) Declare the variable, in the header of an ordinary module ... Global variables are, in general, bad practice. ...
    (microsoft.public.word.vba.general)
  • Re: Exported function mangaled name
    ... extern "C" { ... You have otherwise defined a function unrelated to the header ... You have to declare the function the same way in your .cpp file as in your .h file ... #define LIBSPEC __declspec ...
    (microsoft.public.vc.mfc)
  • Re: Mixed mode woes (hopefully I simply dont get it )
    ... My understanding is that the gcroot template requies managed support, ... It has to be an unmanaged class so that the existing unmanaged ... >> classes can use it and include it's header. ... >> cant declare the managed instance variable in the header because it is ...
    (microsoft.public.dotnet.languages.vc)
  • Re: confused about global namespace and scope
    ... If you want a separate header for your global variables, ... I avoid too many global variables in a C++ app. ... are comparing a UNIX project that you write from scratch with a Windows ... >> You can declare it in any source file. ...
    (microsoft.public.vc.mfc)