Re: Forward declarations

From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/11/04


Date: Tue, 11 May 2004 15:16:33 GMT

On Tue, 11 May 2004 16:35:12 +0200, "Marcin Kalicinski"
<kalita@poczta.onet.pl> wrote:

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

Sure, you can say:

        typedef struct tagMyStruct MyStruct;

and complete the definition later on, if necessary.

>
>Or more generally, can I forward declare a typedef or an enum?

typedefs, I don't think so. Under Comeau, in C mode, if I did something
like this:
        typedef b;
it took it to mean as if I'd said:
        typedef int b;
(and emitted a suitable warning).

With enums, Comeau tells me that forward-declaring them is nonstandard. So
it may be supported, but isn't portable.

> 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).

Well, you began this post by saying "In some headers meant to work with
both C and C++", so there's an obvious reason to prefer struct right there.

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

I would hope that any such header file gives you a documented way to /use/
the facilities it generates, so that such use is immune to future
implementation changes. If you think you're using something that's going to
morph out from under you in the next release, you'll have to get creative
with, perhaps, some preprocessor (or typedef) hackery of your own. Don't
hold your breath waiting for changes in the language to support you here.
        -leor

>
>best regards,
>Marcin
>
>
>

-- 
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix  
C++ users: download BD Software's free STL Error Message Decryptor at:
   www.bdsoft.com/tools/stlfilt.html


Relevant Pages

  • Re-exporting functions from dynamically linked DLL
    ... I have a 3rd party DLL which does not include a LIB file, just a header file ... with typedef declarations for exported functions (It is intended for dynamic ... #define DllExport __declspec ... but I would prefer to just declare it as exported somehow. ...
    (microsoft.public.vc.mfc)
  • Re: Exported function mangaled name
    ... that header file; it must be declared as __declspec ... You have to declare the function the same way in your .cpp file as in ... #define LIBSPEC __declspec ... See my essay on The Ultimate DLL Header File on my MVP Tips site. ...
    (microsoft.public.vc.mfc)
  • Re: make general windows module
    ... The VB version of these declarations is the "Declare Statement". ... mimicing the behavior of a "windows.h" header file for VB. ... not all Windows API calls can be called from VB as some use reference ...
    (microsoft.public.vb.general.discussion)
  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: Trouble with FILE
    ... reasons I had to split up this program in several modules. ... I can declare, ... extern FILE *fp1; ... included in the header file, so that the FILE type is defined. ...
    (comp.lang.c)