What is better when programming a member function?

I have seen member functions programmed both inside the class to which they belong and outside the class with a function prototype inside the class. I only programmed the use of the first method, but I wondered if it is better to use other or only personal preferences?

+4
source share
8 answers

Assuming you have C ++ in mind, it is always better to define functions outside the class, because if you put it inside the class, the compiler may try to embed it, which is not always desirable:

  • Increasing code size (each object file that includes this header may end up with a copy of the function in their code).
  • Violation of binary compatibility when changing the definition of a function.

Even with built-in functions, it is usually better to set definitions outside the class to improve readability of the class’s open interface, unless the function is a trivial accessory or some other single-line interface.

+6
source

For C ++, defining definition methods in a header file means that everything that includes a given header needs to be recompiled when the header changes, even if it's just an implementation detail.

Moving the definitions from the title means that the files containing the title should be recompiled only when the title itself changes (functions added / deleted or declarations changed). This can have a big impact on the compilation time of complex projects.

+4
source

Benefits for both methods.

If you put only prototypes in the class definition, it makes it easy for those who use your class to find out which methods are available. They are not distracted by implementation details.

Entering the code directly into the class definition makes it easy to use the class, you only need to # include the header. This is especially useful (necessary) for template classes.

+2
source

Assuming C ++ language:

The bottom line is that this is a personal preference. Inside the class, it’s shorter overall and more direct, especially for

int getFoo() const { return _foo; } 

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

I saw how to use ...

Of course, non-inlined functions are always out of class.

+2
source

A combination of both styles is often used when defining a class. For simple methods consisting of 1 or 2 lines, it is usually and convenient to define the body of the method in the class definition. For longer methods, it is better to define them externally. You will have more readable class definitions without cluttering them with the body of the method.

Hiding the method implementation is beneficial in that the class user will not be distracted by the actual implementation or make assumptions about the implementation, which may change later.

+2
source

I assume you are talking about C ++.

A good and clean interface is definitely a good idea. Having a separate implementation file helps keep your interface clean.

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

+1
source

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

Thus, it is common practice to define a class in a .h file and implement elements in a .cpp file (usually with the same name).

+1
source

Again, assimilating C ++, I usually limit this to adding to virtual functions, for example.

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

Anything else, and Andrew Medico scores too easily and the compile time hurts.

+1
source

Source: https://habr.com/ru/post/1277427/


All Articles