C ++ learning notes (2) Class Name and implementation separation and inline functions, learning notes inline
I. Classification and implementation Separation:
Similar to c function declaration and implementation Separation
With. h: Class Declaration
. Cpp: class implementation
The. h file of this class should be included in the cpp of a class.
Usage of classes in cpp: for example:
1 // Circle Class 2 // Circle. h 3 class Circle {4 private: 5 double radious; 6 public: 7 Circle (); 8}; 9 // Circle. pp10 # include "Circle. h "11 Circle: Circle () {12 Radius = 1; 13}
To use this class in the main function, you only need to include the. h name in the header file.
Ii. inline functions
Inline functions are defined directly in the class, and inline is used in cpp.
Inline functions should be written in the same file as the class declaration. Otherwise, an error occurs during compilation, which means it is best to write inline functions in the Declaration class. h file, rather than writing it in the Implementation class as a general function. cpp file. Of course, there is another way of writing inline functions, that is, writing them directly in the class. At this time, you do not need to use the "inline" keyword.
Inline functions are just a compilation mechanism. The functions declared in the above two forms only recommend that the compiler inline, and whether the compiler inline is not necessarily. As mentioned above, the overhead of function calling is not negligible for small functions, but can still be ignored for heavyweight functions. In most cases, function calling is common, is the best solution to the problem. Therefore, most compilers do not inline compile functions with loops, recursion, or more code. Some may not even declare them as inline functions.