# Coding conventions with C++

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

## Recommended Posts

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.

##### Share on other sites
It's possible to provide the entire definition in header files, but not advisable as it will increase your compile times significantly.

##### Share on other sites
C++ Heresy. Code sample about halfway down.

##### Share on other sites
Quote:
 Original post by NitageIt'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?

##### Share on other sites
Only if you never modify the header. Otherwise it will likely be even slower.

##### Share on other sites
I thought that functions written out and not just defined in the header files were considered for inlining by the compiler....

##### Share on other sites
Quote:
 Original post by curtmax_0I 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.

##### Share on other sites
If you want to emulate Java... you can do it, but you still need a header file with the definitions.

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;}

##### Share on other sites
Quote:
Original post by Mushu
Quote:
 Original post by curtmax_0I 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.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 11
• 20
• 12
• 13
• 14
• ### Forum Statistics

• Total Topics
632146
• Total Posts
3004433

×