C ++ learning notes (2) Class Name and implementation separation and inline functions, learning notes inline

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.