Sign in to follow this  

Coding conventions with C++

This topic is 3725 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

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


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


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 3725 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