954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

forward declaration instead of header files

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!

tikoti
Light Poster
38 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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
 

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.

kerp
Light Poster
30 posts since May 2010
Reputation Points: 9
Solved Threads: 5
 
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 approachIf 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!

tikoti
Light Poster
38 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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

tikoti
Light Poster
38 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: