• 13
• 15
• 27
• 9
• 9

# Which of these methods do you employ?

This topic is 4957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, im kinda writing my own general library of classes for basic 3d/2d shapes, like quads, panels etc, For more simplicity when managing these files in VC++ i wanna put the entire class in 1 CPP file, like the class def and body. What i wanna know is, is there any performance drop in having to include the whole class rather than just the class def, ie the .H file. ace

##### Share on other sites
To my knowledge the only performance issue caused by that is longer compile times.

##### Share on other sites
yeah thats what i imagined, thanx, can anyone verify this?

ace

##### Share on other sites
That's true, and you'd get a handful linker errors unless you put the code inside the class definition.

##### Share on other sites
well what i want to do is put the code outside the class definition but inside the same cpp file, can this be done?

##### Share on other sites
Yes, this can be done, of course. However, you will probably run into the following errors:

- If you try to use the class somewhere else than in the cpp file it is written in, you will get a compiler error.

- This compiler error can be solved by including the cpp file with the class definition inside the file that needs it. This will cause a linker error, because the member function definitions will appear in two cpp files.

- This linker error can only be solved by not including the member function definitions in a cpp file. This, again, is usually perfomed by 1° writing the member definitions inside the class definition or 2° putting member definitions in a file that does not get included anywhere. You don't want to do any of those, so you're stuck.

This can be summarized as: if you do put everything in a single cpp file, it will compile and link correctly, but you won't be able to use the contents of that file anywhere else.

EDIT: well, actually you can, using the following trick:

In each source file do the following:

#ifdef INCLUDING//all included files for this file here//here, your class definition#else#define INCLUDING//all included files for this file here, again#undef INCLUDING//here, your class definition, again//here, the definitions for the members of your class#endif

In addition to include guards and the like. This works, but takes more time than traditional .cpp/.h implementation and interface separation.

##### Share on other sites
If you want to put it all in 1 file then why insist on it being the .cpp file? It is infinitely better to do that in the .h file. All class methods go inside the definition, and use include guards (as you should).

btw do you mean 'performance drop'? In the compiler, or in your built exe?
If you put it all in the .h file as I just described then there should be no compile time performance hit and it should work nicely.

If you really want it in your cpp file then you will probably have to make the class a template class. However you shouldn't ever be including .cpp files anywhere!

##### Share on other sites
yeah well i hadnt considered pttuing it all in .h files, but ive just gone with the separates now, thanx for all ur help guys

##### Share on other sites
iMalc: what is the real difference between .h and .cpp files, except for their name? ;)