namespace n1 { class c1; } will not work for you?
Whitespace is a user interface thing; C++ cares little for it.
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
Hi all!
I've read forward declaration is much prefered than include in header files mainly because it reduces the compilation time. Other reasons?
My particular situation is that all header files that I use are within a namespace. I've seen two different ways for forward declaration in this case:
namespace n1
{
class c1;
}
and
using namespace n1;
class c1;
I think the first one is better because I don't like the idea of the "using" keyword in a .h file. But with this method 1 line of include is converted into 4 lines for each include
Since I use 5 or more includes of 5 different namespaces the code increases quite a lot.
My question is...
Is there any other way of using this forward declaration but with that increment in source code lines?
any idea?
thank you all in advance!
Your first and second examples are NOT the same thing. In the second example class c1 is not declared to be in the namespace, while in the first example it is.
Lets assume you have this header file
// forward declaration
namespace n2
{
class c2;
};
// this class uses the forward declaration
namespace n
{
class c1
{
public:
private:
n2::c2* pC2; // use forward declaration
};
};
Now in the *.cpp file just do this
#include "test.h"
int main()
{
n::c1 object;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343