C ++ Basic Class Layout

Learning C ++ and looking at a class as follows:

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; } 

I know that Java and methods (functions) in Java are written inside a class. The class looks like a Java interface. I know that I can write a class as follows:

 class CRectangle { int x, y; public: void set_values (int a, int b) { x = a; y = b; }; int area () {return (x*y);} }; 

But is there a difference or a standard?

+4
source share
6 answers

There is a difference. When you write a function definition in a class definition (case 2), it is considered that the function is declared inline. This is standard C ++.

Usage: declare member functions (Java methods) in the class definition in the header file (.h) and define these member functions in the C ++ file (.cpp, .cc or .C, ...) This reduces compilation time, when you change the function body, you only need to compile the C ++ file, whereas if you change something in the header file, all C ++ compilers containing this header must be compiled.

+8
source

This is much cleaner if you only define prototypes in the class definition (which is included in the header file) and implement the methods in the cpp file.

If you have very small classes that do everything in class definition, this may sound simpler because everything is in one place, but as soon as your class grows, any developer using it will hate you, as it will have your code between method prototypes that he can look to find out something (not everyone uses an IDE that shows all the available methods!).

However, there is one exception: the template class methods must be implemented in the header, since they must be compiled for each template specialization.

+5
source

The usual way is to put the implementation of functions in a separate cpp file, which is not included in other files, just compiled. Thus, it is easier to manage dependencies.

If the implementation of the function is indicated in the class definition (as in your second example), this is a hint for the compiler to embed this function (which it may or may not do at the end, depending, for example, on the inside of the function). Generally, functional bodies that are longer than a few lines of code are better placed in the cpp file, since they are probably not going to be embedded, however they may include additional dependencies and clutter the class definition.

The exception is the template functions, in which the bodies must also be in the header, since the compiler must see them in order to create an instance of the template.

+4
source

I believe that if the body of the method is inside the class definition, then it is embedded wherever it is called.

+1
source

In C ++, member functions defined in a class are implicitly inline , member functions defined outside the class are not.

This affects the relationship between your two examples - the linker will complain about several definitions if the first example is #included in several source files, while the second example will not.

In terms of standard / normal:

  • The first form is used for complex classes, where additional headers are required to implement functions.
  • The second form is used for simple classes that do not have additional dependencies.
  • Any form is used for template classes based on developer preferences.
+1
source

There is a slight difference in that the functions defined inside the class are automatically inline . The last definition can be placed in a separate file. Otherwise, there is no difference and preference.

Note that we usually don’t write semicolons after defining functions (although they are allowed inside the class definition).

 void set_values (int a, int b) { x = a; y = b; } // no semicolon here 
+1
source

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


All Articles