Jump to content
  • Advertisement
Sign in to follow this  
dave

Which of these methods do you employ?

This topic is 5082 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
Advertisement
That's true, and you'd get a handful linker errors unless you put the code inside the class definition.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!