C++ classes - declaration/definition
C++ classes - declaration/definition
Hello there, I was thinking, what's the advantages of splitting classes in two files like in C++, beside the fact the code gets a cleaner etc. Am I a bad person if I just use only one file like in Java?
-
- Member
- Posts: 2566
- Joined: Sun Jan 14, 2007 9:15 pm
- Libera.chat IRC: miselin
- Location: Sydney, Australia (I come from a land down under!)
- Contact:
Re: C++ classes - declaration/definition
Probably one of the biggest advantages is that if you modify the class implementation, you don't have to recompile every file that includes it.
For instance, on my project we have some minor class methods implemented in header files which are included by 20-30 different files, and when you modify that implementation you have a fairly large recompile ahead of you
Splitting the class implementation from the prototype can also make it a bit easier to remember the class interface when you need it - rather than trawl through a header littered with implementation you have a nice clean view of the class.
For instance, on my project we have some minor class methods implemented in header files which are included by 20-30 different files, and when you modify that implementation you have a fairly large recompile ahead of you
Splitting the class implementation from the prototype can also make it a bit easier to remember the class interface when you need it - rather than trawl through a header littered with implementation you have a nice clean view of the class.
Re: C++ classes - declaration/definition
Hi,
There is a large reason why. In C/C++ as opposed to Java, there is an explicit preprocessor. What does this mean/do? Well, your "#include" statement, and anything with a "#" before it incidentally such as #ifdef etc are never seen by the compiler itself. (Please pedants for the sake of argument ignore that gcc has a builtin preprocessor as the same principle applies).
So an #include in C or C++ is largely just a text-replace - replacing the #include line with the content of the file you're including. This simplicity is, incidentally, why you need to add include guards in C/C++ (#ifndef MYHEADER_H #define MYHEADER_H etc). When the resultant file gets to the compilation (parse) stage, it can be rather large as it is just one big file containing everything needed.
So, if you put all your class code in the header file and start #including them, the resultant intermediate file passed to the compiler is going to be huge and is going to result in slower builds.
So that's the first reason. The second reason is that header files provide a nice way of having interfaces; What I mean by that is that someone should be able to look at the header file for a class and instantly see what functions it exports and what they do (by comments). The actual implementation of the functions is in the .cc file, and this seperation of interface and implementation is a Good Thing. I'll leave other more zealot C++ programmers to tell you why
So to summarise, yes it's a bad thing, put your code in .cc files.
To the second question, the answer is 100% yesRui wrote:Hello there, I was thinking, what's the advantages of splitting classes in two files like in C++, beside the fact the code gets a cleaner etc. Am I a bad person if I just use only one file like in Java?
There is a large reason why. In C/C++ as opposed to Java, there is an explicit preprocessor. What does this mean/do? Well, your "#include" statement, and anything with a "#" before it incidentally such as #ifdef etc are never seen by the compiler itself. (Please pedants for the sake of argument ignore that gcc has a builtin preprocessor as the same principle applies).
So an #include in C or C++ is largely just a text-replace - replacing the #include line with the content of the file you're including. This simplicity is, incidentally, why you need to add include guards in C/C++ (#ifndef MYHEADER_H #define MYHEADER_H etc). When the resultant file gets to the compilation (parse) stage, it can be rather large as it is just one big file containing everything needed.
So, if you put all your class code in the header file and start #including them, the resultant intermediate file passed to the compiler is going to be huge and is going to result in slower builds.
So that's the first reason. The second reason is that header files provide a nice way of having interfaces; What I mean by that is that someone should be able to look at the header file for a class and instantly see what functions it exports and what they do (by comments). The actual implementation of the functions is in the .cc file, and this seperation of interface and implementation is a Good Thing. I'll leave other more zealot C++ programmers to tell you why
So to summarise, yes it's a bad thing, put your code in .cc files.
Re: C++ classes - declaration/definition
In fact, it makes all sense. Thanks a lot for the fantastic and quik reply
Re: C++ classes - declaration/definition
Worse. You don't have multiple translation units. Whenever you change a single line of code anywhere, you have to recompile everything.pcmattman wrote:Probably one of the biggest advantages is that if you modify the class implementation, you don't have to recompile every file that includes it.
Because either you #include everything together into one big translation unit, or your linker will give you hell about "multiple definitions", as every .cpp file including e.g. your string.cpp will contain the definitions of its functions - which the linker won't accept.
Every good solution is obvious once you've found it.
- gravaera
- Member
- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: C++ classes - declaration/definition
I personally, in my own projects do my headers like this:
I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.
That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
Code: Select all
#ifndef __CLASS_NAME_H
#define __CLASS_NAME_H
//
//Declarations:
//
class foo
{
foo(void);
~foo(void);
int bar(char *);
};
//
//Definitions:
//
foo::foo(void)
{
//Statements
};
foo::~foo(void)
{
//Statements
};
int foo::bar(char *ch)
{
while(ch)
{
//Statements
}
return(1);
};
#endif
That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: C++ classes - declaration/definition
And then you decide to use OGRE, which makes compilation cost an additional 20 secs if you include it somewhere.
Now, you have 10 classes, and you change one line, the time spent compiling the whole lot will take you three and a half minutes.
Enough time to put your office chair in the corridor and play out DND acts.
Now, you have 10 classes, and you change one line, the time spent compiling the whole lot will take you three and a half minutes.
Enough time to put your office chair in the corridor and play out DND acts.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: C++ classes - declaration/definition
Like this: http://www.xkcd.org/303/?Combuster wrote:Enough time to put your office chair in the corridor and play out DND acts.
Re: C++ classes - declaration/definition
holypanl wrote:I personally, in my own projects do my headers like this:I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.Code: Select all
#ifndef __CLASS_NAME_H #define __CLASS_NAME_H // //Declarations: // class foo { foo(void); ~foo(void); int bar(char *); }; // //Definitions: // foo::foo(void) { //Statements }; foo::~foo(void) { //Statements }; int foo::bar(char *ch) { while(ch) { //Statements } return(1); }; #endif
That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
I'd take a look at the arguments presented above by myself, pcmattman and Solar and see if that alters your opinion any.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: C++ classes - declaration/definition
Precompiled Headers to the rescue!Combuster wrote:And then you decide to use OGRE, which makes compilation cost an additional 20 secs if you include it somewhere.
Now, you have 10 classes, and you change one line, the time spent compiling the whole lot will take you three and a half minutes.
Enough time to put your office chair in the corridor and play out DND acts.
I chuck OGRE, Qt and all the other monumentously huge libraries into one header, spend half an hour building that into a 300MB blob, then GCC sails through really quickly
Re: C++ classes - declaration/definition
It is less irritating then to have to scroll trough the whole file everytime, you want to look something up or to change.holypanl wrote: I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.
That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
In addition i have to agree with Solar. "Multiple definitions" is a hell that comes with you if you dont splitt up.
Re: C++ classes - declaration/definition
General rule of thumb: When you're new in town, do as the natives do.
Every good solution is obvious once you've found it.