Sign in to follow this  

Which of these methods do you employ?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Seriously - creating a static library is easy. Create a new static lib project in your IDE, add the sources you need to the library - compile it then link the resulting .lib file to your other projects. It's not that different to compiling another project, except you don't end up with an exe after compilation.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this