Which of these methods do you employ?

Started by
11 comments, last by dave 19 years, 8 months ago
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
Advertisement
To my knowledge the only performance issue caused by that is longer compile times.
yeah thats what i imagined, thanx, can anyone verify this?

ace
That's true, and you'd get a handful linker errors unless you put the code inside the class definition.
well what i want to do is put the code outside the class definition but inside the same cpp file, can this be done?
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.
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!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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
iMalc: what is the real difference between .h and .cpp files, except for their name? ;)
What I'd do, if you're intending to use a reusable libray anyway, is to compile your code into a static .lib file and link it to your oher projects. You'll still have o include the headers though, but if you create a single header that includes the rest and ensure they're in your header search path you'll save a lot of hassle and compile time.

This topic is closed to new replies.

Advertisement