Advertisement

Using 2 C++ source file

Started by February 24, 2003 10:31 AM
8 comments, last by HIPOPO 21 years, 8 months ago
I am programing in c/c++ using Direct X 8.0, and I was woundering if anyone could tell me how to have 2 .cpp files in my project and have it so they both work. I dont like all the code being on one .cpp file it is hard to find my way around the code. I would like to have information on one .cpp so i can use it with the other .cpp file. I would greatly appreciate it if someone could help me. ***never mind I got it to work using headers. Although if you know a better way please letme know.*** [edited by - HIPOPO on February 24, 2003 12:38:14 PM]
most header files are just pre-formatted version of another .cpp file.

so you can put it all in a .h file or..

.h file contains representation of the .cpp file, all function and class and memeber foward declarations and you link your .h file to your matching .cpp file.

personally i just put it all in the .h file.
<=- Talon -=>
Advertisement
Ok, thanks.
Header files should be used for declarations, includes, preprocessor crap, and a few other instances. It''s generally bad practice to use a header file like a source file.

-This is where the world drops off
-ryan@lecherousjester.com
What about the practice of writing your methods for your classes in your .h? Is this genrerally frowned upon? I wasn''t sure if the actual method code should go in the .h, or in it''s on .cpp.
Peon
I''ve noticed different opinions on this. I don''t know if there is a standard, but I''d generally suggest the class declaration in the header, then a matching source file with the method code.

-This is where the world drops off
-ryan@lecherousjester.com
Advertisement
I agree with Run_The_Shadows. This is very common.

Will
------------------http://www.nentari.com
I read on this site and a few other posters confirmed that placing the implementation of functions in a header file will automatically inline said functions.. which I say is a Bad Thing, if I want a function inlined I will tell it to be.

inline being that they are not called as a regular function but that the function's code is pasted into the place where the function was called.

[edited by - Ronin Magus on February 25, 2003 3:09:53 PM]
Nothing gets "Automatically inlined" not even functions with the inline keyword in them. The compiler determines what does/does not get inlined. If it doesn''t think your function should be inlined it won''t do it. And IIRC it''s a standard that inline be used for any function you wish to be inlined or the compiler won''t even attempt to inline it.

As for putting your entire code in the header file there are only a few instances where you should do this. The one I can think of off the top of my head is templates. Because most compilers don''t have template exporting available your entire templated source code must be in the header file for it to work propperly. The rest of the time it''s a pretty good idea to keep the code out of you header file unless it is a simple inlined accessor which is just returning or setting a value. Otherwise it gets too messy.
I''ve read up on it and "Automatically Inline" was not the correct term, as you said. But I was correct that putting functions in the header file will automatically ATTEMPT to inline the functions:

quote:
I don''t like it much, in the interest of implementation hiding (I''m an Object Orientation freak), but I do use it in a lot of my classes lately. The good part is, I don''t have to write in the inline keyword - if you write a function entirely within a class declaration, the compiler will automatically attempt to inline it.


From GameDev itself - http://gamedev.net/reference/articles/article1139.asp

aut viam inveniam aut faciam

MoonStar Projects

This topic is closed to new replies.

Advertisement