Coding conventions with C++

Started by
7 comments, last by edwdig 16 years, 6 months ago
Is it possible or even practical to write your code so that the .CPP file defines the class that is used in other files? Usually you use header files for this and define the function in a .CPP file using the scope operator, but I’ve been doing a lot of Java and C# these days and it would be nice if I could make my C++ code fallow kind of the similar format where the class have populated methods instead of just method definitions. I kind of remember this approach working but not for any large project or library’s and in the end its probably not worth doing but I thought I would ask.
Advertisement
It's possible to provide the entire definition in header files, but not advisable as it will increase your compile times significantly.
C++ Heresy. Code sample about halfway down.
Quote:Original post by Nitage
It's possible to provide the entire definition in header files, but not advisable as it will increase your compile times significantly.


If I did precompiled header files would that reduce the amount of time to what it would be like normally?

Or any downsides to doing this for a large project?
Only if you never modify the header. Otherwise it will likely be even slower.
I thought that functions written out and not just defined in the header files were considered for inlining by the compiler....
Quote:Original post by curtmax_0
I thought that functions written out and not just defined in the header files were considered for inlining by the compiler....

Yup. If they weren't (and were treated like normal functions), you'd have multiple object files with the same function definitions at link time and the linker would puke all over you. Modern compilers probably have some way to not inline and still avoid this problem, but I'm not too familiar with that crazy stuff.
If you want to emulate Java... you can do it, but you still need a header file with the definitions.

Header:
class JavaStyle{public:   static JavaStyle* New();   virtual ~StopJavaTime() =0;   virtual void doStuff() =0;protected:   StopJavaTime(){}};


CPP:
class MyJavaStyle : public JavaStyle{public:   MyJavaStyle()   {   }   ~MyJavaStyle()   {   }   void doStuff()   {      cout << "Ooh, it's a java style class definition, in a cpp file!";   }};JavaStyle* JavaStyle::New(){   return new MyJavaStyle;}
Quote:Original post by Mushu
Quote:Original post by curtmax_0
I thought that functions written out and not just defined in the header files were considered for inlining by the compiler....

Yup. If they weren't (and were treated like normal functions), you'd have multiple object files with the same function definitions at link time and the linker would puke all over you. Modern compilers probably have some way to not inline and still avoid this problem, but I'm not too familiar with that crazy stuff.


Compilers know how to cope with that stuff, as it's necessary due to templates. Every file that you use, say, vector<int> in gets a copy of the code for vector<int> in it. If you've got a good linker (read: no more than a few years old), it recognizes the duplicates and removes them from the final binary.

This topic is closed to new replies.

Advertisement