Re: Re-forward declaration of types which were already forward declared

From: Jonathan Turkanis (technews_at_kangaroologic.com)
Date: 02/15/04


Date: Sun, 15 Feb 2004 12:00:36 -0700


"qazmlp" <qazmlp1209@rediffmail.com> wrote in message:

> Is it a good style to re-forward declare the types, which were
already
> forward declared in base header file, in derived class header file?
>
>
> // base.h
> class addrClass;
> class base
> {
> public:
> virtual void setAddress(addrClass* node);
>
> // Other members
> } ;
>
>
> // derived.h
>
> // Is it a good style to forward declare addrClass here?
> // As setAddress method and hence the parameter of type 'addrClass*'
> // is from the base class, it may not be required, I suppose.
> class derived : public base
> {
> public:
> virtual void setAddress(addrClass* node);
>
> // Other members
> } ;
>

Obviously, you need to include "base.h" here, at least indirectly. It
is not necessary to forward declare addrClass, sinmce a forward
declaration will already be included.

Under some circumsances it is good programming practice to forward
decalre a class decalred by an included header, for instance, if
addrClass was just an implementation detail in "base.h" which might be
subject to change. Here it is part of the interface of a public
virtual function defined in the base class, so I don't see any reason
to redeclare it.

> And, similarly, what about #include-ing the header which has to be
> added
> only because of the Base class method signatures?

Which header? The header containing the definition of addrClass?
Definitely not needed in your example. Never include more headers than
absolutely necessary.

Jonathan



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)