views:

265

answers:

8

I have seen member functions programed both inside of the class they belong to and outside of the class with a function prototype inside of the class. I have only ever programmed using the first method, but was wondering if it is better practice to use the other or just personal preference?

+5  A: 

Assuming you mean C++, it is always better to define functions outside of the class, because if you put it inside the class, compiler may try to inline it, which is not always desirable:

  1. Increase in code size (every object file that includes this header might end up with a copy of the function in their code).
  2. Breaking binary compatibility when function definition changes.

Even with inline functions, it is usually better to put definitions outside the class to improve readability of class public interface, unless the function is a trivial accessor or some other one-liner.

Alex B
Whether a member function is implemented inside the class has no relevance for whether the compiler will attempt to inline it.
Franci Penov
Sorry, this is not correct: http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.8
Alex B
I should correct myself - there might be compilers that automatically inline members implemented inside the class definition. However, this behavior is compiler-specicif and cannot be assumed to be true for all C++ compilers.
Franci Penov
I might be wrong; however, I will accept your claim, if you point me to the relevant section in the C++ standard, not a random C++ FAQ on the internet.
Franci Penov
That's why I said you should put the implementation outside of the class if you care about binary compatibility of the library interface, since putting definition outside of declaration is the only case where code generation will be deterministic. Perhaps I should change "will" to "may".
Alex B
There you go: ISO/EIC 14882: section 7.1.2/3
Alex B
I stand corrected.
Franci Penov
In Visual Studio in the release configuration at least, making a function inline by putting it inside the class or writing "inline" is only a hint. The compiler decides by itself whether to inline a function.
Lev
Binary compatibility won't break because all relevant files will be recompiled. However, the readability argument is correct.
Lev
Lev, this is only the case when you have the control over the code that uses this class (in the case if you are a library developer, you usually don't).
Alex B
+2  A: 

There's advantages to both techniques.

If you place only prototypes in the class definition, that makes it easier for someone who is using your class to see what methods are available. They aren't distracted by implementation details.

Putting the code directly in the class definition makes it simpler to use the class, you only have to #include a header. This is especially useful (necessary) with templated classes.

Ferruccio
Implementation details is a big part of this.
Kris Kumler
+2  A: 

Presuming the language is C++:

The bottom line is that is personal preference. Inside the class is shorter overall and more direct, especially for the

int getFoo() const { return _foo; }

type of function. Outside te class, can remove "clutter" from the class definition.

I have seen both in use...

Of course, non-inlined functions are always outside the class.

Andrew Stein
+1  A: 

I assume you are talking about C++.

Having a nice and clean interface is certainly a good idea. Having a separate implementation file helps to keep your interface clean.

It also reduces compilation time, especially if you are using an opaque pointer.

Martin Cote
+2  A: 

It is also common to mix both styles when defining a class. For simple methods consisting of 1 or 2 lines it is common and convenient to define the method body within the class definition. For more lengthy methods it is better to define these externally. You will have more readable class definitions without cluttering them up with the method body.

Hiding the implementation of a method is beneficial in that the user of the class will not be distracted by the actual implementation, or make assumptions about the implementation that might change at a later time.

mhawke
+1  A: 

If you implement the function inside the class, you cannot #include the class in multiple .cpp files or the linker will complain about multiple definitions of the function.

Thus, usual practice is to have the class definition in a .h file and the members implementation in a .cpp file (usually with the same name).

Franci Penov
+4  A: 

For C++, putting method definitions in the header file means that everything that includes a given header must be recompiled when the header changes - even if it's just an implementation detail.

Moving definitions out of the header means that files which include the header will need to be recompiled only when the header itself changes (functions added/removed, or declarations changed). This can have a big impact on compile times for complex projects.

Andrew Medico
+1  A: 

Again, assiming C++, I usually restrict this to placeholders on virtual functions, e.g.

virtual int MyFunc() {}  // Does nothing in base class, override if needed

Anything else, and Andrew Medico's point kicks in too easily and hurts compile times.

Shane MacLaughlin