Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re-forward declaration of types which were already forward declared

Reply
Thread Tools

Re-forward declaration of types which were already forward declared

 
 
qazmlp
Guest
Posts: n/a
 
      02-15-2004
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
} ;


And, similarly, what about #include-ing the header which has to be
added
only because of the Base class method signatures?
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-15-2004
"qazmlp" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a template to 'new' a forward declared class Ole Nielsby C++ 8 05-21-2007 10:09 AM
declared or not declared ? JohnZing ASP .Net 3 02-05-2006 08:38 PM
Error 500: ERROR: Cannot forward. Writer or Stream already obtained. Pooja Chavan Java 1 03-01-2005 06:43 PM
Error 500: ERROR: Cannot forward. Writer or Stream already obtained. Error JavaQueries Java 1 03-01-2005 06:30 PM
declaring a function only if it isn't already declared nospam_timur@tabi.org C Programming 2 09-18-2004 03:47 PM



Advertisments