Page 1 of 1
Forward Declaration VS. Header Declaration
Posted: Sun Dec 27, 2009 9:49 pm
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.
Re: Declaring Class VS. Including Header Declaration
Posted: Mon Dec 28, 2009 12:13 pm
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.
Re: Declaring Class VS. Including Header Declaration
Posted: Mon Dec 28, 2009 4:35 pm
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...
Re: Declaring Class VS. Including Header Declaration
Posted: Mon Dec 28, 2009 5:19 pm
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.
Re: Declaring Class VS. Including Header Declaration
Posted: Mon Dec 28, 2009 5:41 pm
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.
Re: Declaring Class VS. Including Header Declaration
Posted: Thu Dec 31, 2009 10:36 am
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.
Re: Declaring Class VS. Including Header Declaration
Posted: Thu Dec 31, 2009 4:05 pm
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 =/.