Const member functions of C ++ class, default constructor, and copying form parameters to call Functions

Source: Internet
Author: User

Const member functions of C ++ class

Double Sales_item: avg_price () Const
{
}

The const keyword indicates that this is a const member function and it cannot modify the member variables of the sales_item class.

 

If no constructor is defined for a class, the compiler will automatically generate a default constructor for the class to become the "default constructor for synthesis ". In this case, the constructor will not automatically initialize members of the built-in type. For members of the class type, such as string, the default constructor of string is used for initialization.

 

When you call a copy function, you do not need to consider whether the parameter is not Const. The function is only a copy. The function cannot change the real parameter. The result is that the const object can be passed to the cosnt parameter or non-const parameter. There is no essential difference between the two. It is worth noting that the equivalence of the const parameter mentioned here is only applicable to non-reference parameters. It is not applicable to references and pointers. C ++ primer p229.

Related Article

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.