Static member functions cannot be modified with const or accessed with nonstatic data.

Source: Internet
Author: User

The reason why static member functions in C ++ cannot be modified using static:

The fifth meaning of static in C ++: use static to modify class member functions that do not access non-static data members. This means that a static member function can only access its parameters, static data members of the class, and global variables.

The reason const cannot be used: the value accessed by a static member function is its parameter, static data member, and global variable, and the data is not part of the object state. The use of the keyword const in a member function indicates that the function does not modify the data member of the target object accessed by the function. Since a static member function does not access non-static data members at all, there is no need to use Const. When do I use static data members and static functions? Define the data member as a static variable to indicate that the global data logically belongs to this class. Define a member function as a static function to indicate that the global function logically belongs to this class, and the function only operates on static data, global data, or parameters, do not operate on non-static data members. The first meaning of static: when modifying a global variable, it indicates that a global variable is only visible to functions defined in the same file. Static indicates that the value of a local variable is not lost because of function termination. The third meaning of static: when modifying a function, it indicates that the function is called only in the same file. The fourth meaning of static: Modify the data member of the class, indicating that there is only one instance for all the objects in the class. That is, the instance is shared by all objects. Static: modifies a class member function.
For example:
Class AAA {public: static void display (void) const {printf ("m_a1 value is % d \ n", m_a1); // if no const exists, run OK. // If const exists, the following error is returned. // error c2272: 'display': modifiers not allowed on static member functions // Question 1: // The modifier cannot be used for static member functions. // The modifier is not modified. // What does the error message mean ?} Static int m_a1 ;}; int AAA: m_a1 = 100; int _ tmain (INT argc, _ tchar * argv []) {AAA: Display (); Return 0 ;}
It should at least be said in the book that this usage of const (indicate will not modify the content of the current object ),
It can only be used for general member functions of classes, but not static member functions.

What are the differences between static member functions? Basically, there is no this pointer in the static member function,
That is to say, it is equivalent to a common (non-member) function defined in this class,
This is why static member functions cannot access member variables,
Therefore, a static member function does not exist at all to change the content pointed to by this,
Conclusion (at last): the const modifier is meaningless for static member functions.

Because static members are not part of any object, static members cannot be declared as Const. After all, declaring a member as const promises not to modify the object to which the function belongs.

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.