Why / when should methods be declared outside my class using the scope operator in C ++?

I come from a .Net background, and I'm starting to learn C ++, I saw in the tutorial here that you can define the content of a function outside the class via the scope operator :: .

Here is an example in the tutorial:

 // classes example #include <iostream> using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; } 

All I can find on Google is region resolution, but I would like to know why / when to use an declaration of type set_values instead of an declaration inside a class such as area . Are there any benefits, rules, best practices?

+4
source share
4 answers

This can only be done if you want to put the implementation of the function in a separate file - basically .cpp

Header files almost always have the extension .h. The purpose of the header file is to store ads for other files. You basically define here what the class will look like - the definition of functions. Useful if you need a lot of activities. Use .cpp to go into the details β€” to actually implement the functions that were β€œdefined” in the header file.

If many classes are involved, using header files and cpp will reduce build time. If you feel very brave and want to reduce assembly time even further, I suggest reading the PIMPL template information.

You can read more about the basic information here.

+2
source

The main advantage of declaring separation and implementation is that you create code that will be distributed to end users, for example. library.

When you distribute the code, you only distribute the header file and the * .o object file. So your supernatural secret code is safe, compiled inside the .o file, and the end user can still use it because it has a .h file, co linker knows where to look.

+1
source

This is a really good question, and it all comes down to compilation times. People from von .NET face some difficulties understanding the difference between #include and, for example. C # using . What you need to keep in mind:

The #include directive copies the contents of an attached file.

Thus, if you put the implementation code in your header, you essentially compile it several times, once on each #include site (and the linker will have to remove the duplicate code). In addition, if you modify the cpp file, it will be recompiled alone. If you change the header file, all files, including it (also indirectly!), Will be recompiled.

So put as many internal implementation files (i.e. cpp) as possible . Compilers have become quite good, since optimizing the time code of links, the fact that this can prevent inlining is less of a problem than before.

This is not possible with templates, and this explains most of the very long compilation periods that you can see when using C ++.

+1
source

The point here is to separate the definition, declaration, and implementation of class methods. This is very useful if you want to put them in different files (as a rule, the heading contains the class definition and the .cpp class implementation), or even choose for consistency or for other reasons not to have everything directly in the class.

0
source

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


All Articles