Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

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.

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).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top