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!

namespace n1 { class c1; } will not work for you?
Whitespace is a user interface thing; C++ cares little for it.

If you don't want to have the using directive in the header file you can just use class n1::c1; that's the preferred solution as far as I know.

namespace n1 { class c1; } will not work for you?
Whitespace is a user interface thing; C++ cares little for it.

Yes, I think I will do something like that. Though I don't like it much. I was just wondering is there was another approach

If you don't want to have the using directive in the header file you can just use class n1::c1; that's the preferred solution as far as I know.

That would be a clean solution but unfortunately does not compile with mi gcc

Thank you both!

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;
}

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.

You are absolutely right, it is good to know more about this topic which I ignored before

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.