Member function definition

天涯浪子 提交于 2019-12-07 02:53:25

问题


What is the right approach to take:

Define the member (class) function inside the class?

Define the member (class) function outside the class?

Thanks.


回答1:


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




回答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.




回答3:


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.




回答4:


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"




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/4837267/member-function-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!