Constant type problem of static member functions in C + +

Source: Internet
Author: User

These days looking at the C + + object model in depth, see a sentence: Static member functions can not be constant functions, the following code:

Class Test {
	virtual ~test ();
Public:
	static void Statest ();
If Statest () is modified to: static void Statest () const;
Compile return error in VS2015: ' statest ': modifiers not allowed on static member functions.

The root causes are:

1. The const member function means that in the body of the function, the member variable of the class object is not allowed to be modified, which is to say that the this pointer of the member function is const* in the const member function, that is, a pointer constant that does not allow the pointer to modify what the pointer points to.

2. Again, the meaning of the static member function is that the member function belongs to the entire class, not to a particular class instance, and, more bluntly, does not require the this pointer (a member function called by a class instance to be converted to the this pointer into a member function).

The reason for this is obvious, because these two keywords are conflicting, and in a function without this pointer, it is completely meaningless to attempt to specify the constants of this pointer.


The daily product Bujikuibu, even thousands of miles.

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.