Forward Declaration VS. Header Declaration

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Forward Declaration VS. Header Declaration

Post by XianForce »

Okay, well I'm wondering if there's much of a difference between the two methods of declaring a class in a file:

Code: Select all

//myClass.h
class myClass
{
//some stuff in here...
};


//myOtherClass.h
#include "myClass.h"

class myOtherClass
{
//some stuff here...
};

Or

Code: Select all

//myClass.h
class myClass
{
};


//myOtherClass.h

class myClass;

class myOtherClass
{
};


//myOtherClass.cpp
#include "myClass.h"

//some other stuff...

Is the second one ever going to bring up problems? I know that the first method will occasionally cause an error, because of the order that files are compiled in, I believe, which is why sometimes the second method is often used.

I was thinking of just using the second method as a 'practice', but before I do, I want to find out if it's going to screw anything up later.

So, thanks in advance for any responses =p.
Last edited by XianForce on Mon Dec 28, 2009 5:17 pm, edited 2 times in total.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Declaring Class VS. Including Header Declaration

Post by dandymcgee »

Really, what you should be aiming for is not which of those to use, but how to design your program's structure so that you never have use either of those. Circular dependancies are a bitch and if you can at all avoid them I would highly recommend doing so.

I also understanding avoiding them isn't always possible without refactoring major portions of your project (something nobody wants to do), so as for which method I use, the latter. They're both the same thing though from what I can see.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Declaring Class VS. Including Header Declaration

Post by XianForce »

dandymcgee wrote:Really, what you should be aiming for is not which of those to use, but how to design your program's structure so that you never have use either of those. Circular dependancies are a bitch and if you can at all avoid them I would highly recommend doing so.

I also understanding avoiding them isn't always possible without refactoring major portions of your project (something nobody wants to do), so as for which method I use, the latter. They're both the same thing though from what I can see.
Unless you put everything into a single file, I don't see how you could get around using one of those (unless there's some other method I haven't seen).

The former just seems a bit cleaner to me, but then when I start coming to places where I have to use the latter, I start questioning whether I should even use the former at all...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Declaring Class VS. Including Header Declaration

Post by dandymcgee »

XianForce wrote:
dandymcgee wrote:Really, what you should be aiming for is not which of those to use, but how to design your program's structure so that you never have use either of those. Circular dependancies are a bitch and if you can at all avoid them I would highly recommend doing so.

I also understanding avoiding them isn't always possible without refactoring major portions of your project (something nobody wants to do), so as for which method I use, the latter. They're both the same thing though from what I can see.
Unless you put everything into a single file, I don't see how you could get around using one of those (unless there's some other method I haven't seen).

The former just seems a bit cleaner to me, but then when I start coming to places where I have to use the latter, I start questioning whether I should even use the former at all...
Sorry I didn't read the comments right. Usually you use separate code boxes for each file.

The first method is the correct method and you should always use that when you can. The second method is the circular dependency fix, which you should try to avoid to begin with.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Declaring Class VS. Including Header Declaration

Post by XianForce »

dandymcgee wrote:
XianForce wrote:
dandymcgee wrote:Really, what you should be aiming for is not which of those to use, but how to design your program's structure so that you never have use either of those. Circular dependancies are a bitch and if you can at all avoid them I would highly recommend doing so.

I also understanding avoiding them isn't always possible without refactoring major portions of your project (something nobody wants to do), so as for which method I use, the latter. They're both the same thing though from what I can see.
Unless you put everything into a single file, I don't see how you could get around using one of those (unless there's some other method I haven't seen).

The former just seems a bit cleaner to me, but then when I start coming to places where I have to use the latter, I start questioning whether I should even use the former at all...
Sorry I didn't read the comments right. Usually you use separate code boxes for each file.

The first method is the correct method and you should always use that when you can. The second method is the circular dependency fix, which you should try to avoid to begin with.
ooo Thanks, that's all I needed =p.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Declaring Class VS. Including Header Declaration

Post by Falco Girgis »

dandymcgee wrote:
XianForce wrote:
dandymcgee wrote:Really, what you should be aiming for is not which of those to use, but how to design your program's structure so that you never have use either of those. Circular dependancies are a bitch and if you can at all avoid them I would highly recommend doing so.

I also understanding avoiding them isn't always possible without refactoring major portions of your project (something nobody wants to do), so as for which method I use, the latter. They're both the same thing though from what I can see.
Unless you put everything into a single file, I don't see how you could get around using one of those (unless there's some other method I haven't seen).

The former just seems a bit cleaner to me, but then when I start coming to places where I have to use the latter, I start questioning whether I should even use the former at all...
Sorry I didn't read the comments right. Usually you use separate code boxes for each file.

The first method is the correct method and you should always use that when you can. The second method is the circular dependency fix, which you should try to avoid to begin with.
I've actually heard differently. If you can get away with just prototyping and not including another header file, you should go with that. Otherwise you're creating a dependency on header B for header A. Now if you change anything in header B, header A has also changed, and everything including header A must be recompiled.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Declaring Class VS. Including Header Declaration

Post by XianForce »

[quote=GyroVorbis]I've actually heard differently. If you can get away with just prototyping and not including another header file, you should go with that. Otherwise you're creating a dependency on header B for header A. Now if you change anything in header B, header A has also changed, and everything including header A must be recompiled.[/quote]

Ahh, that's good to keep in mind; But I'm using functions from these classes, so I gotta include the header =/.
Post Reply