Member Function Definition

What is the right approach:

Define a member (class) function inside a class?

Define member (class) function outside class?

Thanks.

+6
c ++ function definition class member
source share
6 answers

Assuming you are talking about these three possibilities:

  • The method defined in the class definition in the header file.
  • The method determines the definition of the outer class in the header file.
  • The method determines the definition of the outer class in the implementation file.

Then the project and company management can force you to use (1) or (3) always.

When you have a choice, IMHO is best adapted to the circumstances, given things like

  • Do you need a module for headers only? Then (1) by default, (2) is possible.
  • Is the method a big beast? Then (2) or (3).
  • Template Method Specialization? Then (2) or (3).
  • Is there a build time problem (slow builds)? Indicates (3).
  • Template class? (1) or possibly (2)

But unless the choice is actually imposed on you, first consider the clarity of your code.

Cheers and hth.,

+9
source share

If the definition of a member function is trivial (in an informal sense) and does not introduce any additional dependencies, I would usually define a member function outside the class body in a separate source file.

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

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

+2
source share

The general advice is to keep the headings as simple and clean as possible. Headers will be included with external code, and they will have to process everything that you wrote there. If you write a method in the header, all translation units will compile this function only so that the linker can abandon all but one of them later.

If your code has an internal dependency on a type or library that is not part of your interface, then by inserting the code of a member function into the class declaration, the definition of this class or the headers of this library will be included in your header, which means that you skip your implementation data to your users.

+2
source share

You mean "in the class declaration /.h file is" vs "in the .cpp file using ::"?

If so, I will always look for the latter. When it comes to debugging, it’s much easier to go through and see what happens. It also helps declutter declare a class that doesn't need any implementation details. "

+1
source share

If you want to define a function inside a class, the most general syntax looks like:

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

If you want to define a function outside , it is similar to declaring functions in front of the 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 the included file, you can determine:

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

Some classes are places in the header file and definitions in the cpp file used by this header. Various methods are possible.

Both of these approaches are valid. Often I will include very small and / or basic class functions directly in the class and other functions that do the harder work that I usually share. Try to think about the difference in coming to your code and want to change it.

+1
source share

if we see according to a performance problem, than this is a more efficient way to declare a function in a class. because at compile time it combines all the functional calls and other components, so it will be lightweight and should be faster to get everything in one source ...

0
source share

All Articles