C++ question?

Miscellaneous Forums/General Discussion/C++ question?

Hey guys, I have a question about C++.

I've been learning it for a while now and I'm trying to get a better understanding of classes. I notice that some tutorials I look at create the member functions outside of the class description.

class myClass{
void myFunction();
};

myClass::myFunction(){
}


And then other tutorials put the function right in the class.

class myClass{
void myFunction(){
  //do stuff
}
};




My questions is, what's the difference? Is there advantages to either approach?

the second one is neater and easier to see where things belong.

There are a few reasons to declare functions outside of the class definition:

- It can make the class definition an easy to read outline when all the code isn't packed into it

- Sometimes you'll need to move the function outside of a class definition if it needs to access things (like other classes) that aren't defined yet. In this case the function needs to be below the declarations of whatever classes it makes use of.

- When you move function definitions into .cpp files rather than .h files, other files can #include the .h files without having to compile all the function code from scratch every time - it just compiles the .cpp file once and uses the linker later to link everything together. As a general rule it's best to keep the size of .h files to a minimum for optimal compile efficiency (otherwise a large project can take many minutes to compile).

And one reason for declaring it inside:

- Some function modifiers like "inline" and "template" require that your code be embedded in the declaration like your second example. This is because inline and template code are generated during the compile stage rather than the link stage (for example inline functions are embedded into the code that calls them without actually calling them - it just copy-n-pastes the code)

As the complexity of your project increases, you'll probably find why the general C++ programming standard simply puts declarations in .h files and all the major code in .cpp files whenever possible - this way you avoid a lot of cross-reference and forward-declaration issues, and it speeds up the compile process.

the first one also allows you to spread your methods a cross several files. ie define in a .h file and implement in a cpp file.

I prefer the first one. It's easier to see what methods are available for a given class - if there are a lot of them.

@EdzUp, John J., REDi, Brucey:
I don't want to be smarty but you are all wrong.

class myClass{
void myFunction(){
//do stuff
}
};



The above implementation forces the compiler (according to standard rules of C++) to repeat the code every time will make a function call to myFunction (inline). This leads to larger executable.
Usually you have to use this method only for short functions to improve speed execution whenever you think so.
Otherwise you must follow the:


myClass::myFunction(){
}


implementation

The above implementation forces the compiler (according to standard rules of C++) to repeat the code every time will make a function call to myFunction (inline). This leads to larger executable.

Wrong. It completely depends on the compiler and settings. Most compilers require (by default, at least) that the "inline" keyword be added to the function, otherwise it will not be inlined in any case. In fact, some compilers disable inline code generation completely by default (even if you do have the "inline" keyword), and some don't even support it.

I can quote from the ANSI C++ standard that defines inline behavior if you like.

@EdzUp, John J., REDi, Brucey:
I don't want to be smarty but you are all wrong.

I don't see how you can say they're wrong. EdzUp and Brucey stated that they prefer the first technique, and have valid points about it increasing readability (which I agree with). Certainly not wrong.

REDi stated that the non-inline method allows you to spread your function definitions across multiple files. He is absolutely correct, although I wouldn't recommend it.

Don't worry about appearing "smarty" - you've already eliminated that possibility.


Don't worry about appearing "smarty" - you've already eliminated that possibility.



these forums always bring a smile to my face :P

Of course you can have a different setup for your compiler in order to behave differently, but I mentioned that this is the behavior of the original C++ rules.
I said 'wrong' because Craig asks about the difference from the programming point of view and not how he will manage visually his files or his development environment.

REDi stated that the non-inline method allows you to spread your function definitions across multiple files. He is absolutely correct, although I wouldn't recommend it.


Do you think that an answer like this reveals the truth to a new programmer?
This is the tree and not the forest.
Having inline functions in C++ you are spreading your code inside the binary file and this is the very important to a new comer to the world of C++

@Kev: I said that I am not smarty and I insist. Judging from your smile I can say that I see an irony. A word about the subject could be more useful from your smiling.

Except inline is just an hint to the compiler (just like "register") who may - and often will - ignore it. And THAT's standard.

Except inline is just an hint to the compiler (just like "register") who may - and often will - ignore it. And THAT's standard.

Exactly. Fortunately a good compiler will inline most if not all of the functions you mark with "inline", so it's good practice to do so (do NOT assume they'll be inlined simply because you put the definition in the class declaration).

Having inline functions in C++ you are spreading your code inside the binary file and this is the very important to a new comer to the world of C++

The original question was what's the difference / advantages to either defining the function code inside or outside of the class definition. By the current C++ standard, unless you put an "inline" keyword on your function declaration, you have to assume it's not going to be inlined, so in this case this has nothing to do with inline functions.

The difference then is simply up to preference, and the reasons I mentioned in my first post (reducing problematic cross-referencing and forward-declarations, increased compile speeds).


By the current C++ standard, unless you put an "inline" keyword on your function declaration, you have to assume it's not going to be inlined, so in this case this has nothing to do with inline functions.


I don't have the 'official' c++ spec on hand, but this from Stroustrup's 'bible' suggests otherwise (10.2.9: In-class function definitions):

A member function defined within the class definition - rather than simply declared there - is taken to be an inline member function.


This is the way I've always treated in-class method defs, and it makes perfect sense: the only reason you'd really want to provide the source code to a function in a header file is for inlining purposes anyway.

Also, inlining is only a hint - compilers are free to inline nothing, stuff you mark 'inline', or even everything. However, in practice compilers do tend to honor 'inline' quite well.

So, to answer the original question, the only *effective* difference between having your function definitions inside/outside class definitions, is that function definitions inside a class will be automatically 'inline' - ie: there's no difference between this:
struct Blah{
  void Etc(){ cout<<"Hello mum"<<endl; }
};

...and this...
struct Blah{
  void Etc();
};
inline void Blah::Etc(){ cout<<"Hello mum"<<endl; }

However, as others have noted there are also certainly maintenance/organizational advantages in *not* having all your function definitions in your header files - not to mention compile time advantages!

function definitions inside a class will be automatically 'inline' - ie: there's no difference between this:

...

...and this...

Like I said, it depends on your compiler and compiler configuration. Right now my project in Visual C++ 2005 is set to the behavior (default) of inlining only functions marked with the inline keyword. This can be changed easily to either inline nothing or inline everything possible from the project properties.

Even in a purely theoretical point of view, it's proper to use the "inline" keyword to mark any functions desired to be inlined, otherwise, what's the point of having the keyword at all? Just because it's good programming practice, I always mark small performance critical functions with "inline" because not only does this make the intended use of the function more clear, but it ensures the chances of it actually being inlined in the event I port to a different compiler.

The thing about C/C++ is that there are so many different ways to achieve the same thing. Obviously everyone has their own preference, but I've noticed that many people are adopting many of the same techniques that really help readability and maintainability in the long term, including the use of the "inline" keyword to mark all functions desired to be inlined (duh).

[sarcasm]

Do think that an answer like this reveals the truth to a new programmer?

Yeah sure, its a truth as well as being a difference and an advantage, and yes its a tree (if you like), but it takes lots of trees to make a forest.

BTW Moraldi, your code example...
myClass::myFunction(){
}

doesn't conform to the (cough) original C++ rules and is actually forbidden ;)

[/sarcasm]

jk :)


Like I said, it depends on your compiler and compiler configuration.


No it doesn't.

Placing a function definition inside a class definition is *semantically* equivalent to using the 'inline' keyword.

What your compiler does or doesn't do with functions marked 'inline' (explicitly or implicitly) is another matter altogether.

Placing a function definition inside a class definition is *semantically* equivalent to using the 'inline' keyword.

So doing that declares it as "inline" implicitly? I'll have to look that up.

Even if that's true, it's still bad practice IMO to omit the "inline" keyword when "inline" behavior is assumed. Most well designed and structured C++ code I've seen explicitly declares inline functions with the "inline" keyword, and this makes sense.

What the compiler does with inlining is a grey area, the main reason to use inline is to save having to write the same lines of code over and over again.

This topic shows that every programmer has thier own style to coding :)

Me I prefer to have code within the class declaration as its easier for me to read, in the old days when 640k of ram was a lot it was a problem with file sizes but in todays Terrabyte drives its not really an issue.