C + + Fundamentals--static members and const members

Source: Internet
Author: User

We can modify the member function of a class with static, or you can use the const to decorate a member function of a class (written at the end of the function to indicate that the member variable cannot be modified, not the one that represents the return value as a constant). Can you use both static and const to modify the class's Crown function?

No. In order to ensure that the function cannot modify the state of the instance of the class when implementing the Const member function, the C + + compiler adds an implicit parameter, const this*, to the function. However, when a member is static, the function does not have this pointer. This means that static usage and static are conflicting.

The function of static is to indicate that it is only on the static variable of the about, which is not related to the instance of the class, and that the function of const is to ensure that it cannot modify the state of the instance of the class, and that it has no relation to the static variables of the type.

A function in this class is called with a null pointer of a class, but this function does not use the class's member variables, then it can be successful, if the call function uses the class member variable, then it will crash, if the virtual function is called, there will be problems, because the virtual function is accompanied by the instantiation of the resulting. There is no virtual function table without instantiation.

Can a static member function also be a virtual function?

No, call a static member function without an instance. But calling a virtual function requires a pointer to a virtual function table from one instance to get the address of the function, so calling a virtual function requires an instance.

In C + +, member variables are initialized in the same order as they are declared in a variable class, regardless of their order in the constructor's initialization list.

The copy constructor cannot pass the value, only the reference, if it is a value, then in the function of the constructor must use the function itself, this is unreasonable phenomenon.

C + + Fundamentals--static members and const members

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.