6

What is the right approach to take:

Define the member (class) function inside the class?

Define the member (class) function outside the class?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 1
    I'm not sure if you mean inside the class body, but if you do this could be a worthwhile read: http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.8 – Skurmedel Jan 29 '11 at 14:13
  • 1
    You may wish to check [a similar question about constructors](http://stackoverflow.com/questions/4761834/defining-constructor-in-header-file-vs-implementation-cpp-file), basically the answers and the reasoning stays the same. – Sergei Tachenov Jan 29 '11 at 14:52
  • @ Skurmedel. Yes, in the class body – Simplicity Jan 29 '11 at 16:17

6 Answers6

9

Assuming you're talking about these three possibilities:

  1. Method defined in class definition in header file.
  2. Method define outside class definition in header file.
  3. Method define outside class definition in implementation file.

Then project and company guidelines may force you to use (1) or (3) always.

When you have a choice, it's IMHO best to adapt to circumstances at hand, considering things such as

  • Do you want a header-only module? Then (1) as default, (2) possible.
  • Is the method a large beast? Then (2) or (3).
  • Template method specialization? Then (2) or (3).
  • There is a build-time problem (slow builds)? Indicates (3).
  • Template class? (1) or possibly (2)

But except where the choice is effectively forced on you, above all consider the clarity of your code.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

Unless the member function definition is trivial (in an informal sense) and doesn't introduce any additional dependencies I would normally define a member function outside of the class body in a separate source file.

It's often a matter of style but there are some cases in which it is necessary and many other cases in which it is desirable to define function outside of the class body.

For example, in the cases where you have interdependent classes and only a forward declaration of another class can be made available before the class definition, a member function which uses the definition of that other class can only be defined outside of the class body after a full definition of the other class has been provided.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • +1, as it brings the important decisions, but it is not a matter of style. By providing the implementation in the class declaration you are forcing your dependencies (implementation details) on your users (i.e. if you use some library to perform an specific calculation, users of your class will depend on it), and it will also have the practical effect of forcing all translation units that include your class to compile all your methods, increasing compilation times. – David Rodríguez - dribeas Jan 29 '11 at 14:18
  • @DavidRodríguez: Well, there's such a thing as bad style - even very bad style - and where both are possible and 'correct' it _must_ be a style decision, surely? – CB Bailey Jan 29 '11 at 14:21
  • That is why I did upvote :) Your answer brings the most important things that you have to consider into scope. – David Rodríguez - dribeas Jan 29 '11 at 14:25
2

A common advice is to keep headers as simple and clean as possible. Headers will be included by external code, and they will have to process everything that you have written there. If you write a method in the header, all translation units will compile that function, only so that the linker can discard all but one of them later on.

If your code has an internal dependency on a type or library that is not part of your interface, then by inlining the code of the member function in the class declaration the definition of that class or the headers of that library will have to be included in your header, and that means that you are leaking your implementation details to your users.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

Do you mean "in the class declaration / .h file" vs "in a .cpp file using ::" ?

If so I always go for the latter. When it comes to debugging, it's a lot easier to step through and see what's going on. It also helps declutter the class declaration, which doesn't need to know any implementation details"

Dave
  • 3,438
  • 20
  • 13
1

If you want to define a function within a class the most basic syntax looks generally like:

class Object
{
  int property;
  void doSomething()
  {
      property=100;
  }
};

If you want to define a function outside it is similar to declaring functions before main and in library files. In your class you have:

class Object
{
  int property;
  void doSomething();
};

Then somewhere after your class, after the main() function or in an included file you can have the definition:

void Object::doSomething()
{
  property=100;
}

Some place classes in a header file and the definitions in a cpp file used by that header. Various techniques possible.

Both of these approaches are valid. Often I will include very small and/or core class functionality directly within the class and other functions which do heavier bulk work I tend to separate. Try to think the difference in coming upon your code and wanting to alter it.

0

if we see according to performance issue than it is more effective way to declare the function in the class . becouse at the compile time it conects all the funcation calls and other components so it will easy and must be faster to get all in one source...

ram singh
  • 1
  • 4
  • In general, the generated code from methods defined inside the class body and outside will be the same. No difference in performance. – Zane Oct 28 '12 at 21:37