Re-forward declaration of types which were already forward declared

Q

qazmlp

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

Jonathan Turkanis

qazmlp said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top