Static member functions

Source: Internet
Author: User

A static member function is a member function of a class. It does not belong to any object applied for by the class, but a function shared by all the members of the class.

 

Like static data members, static member functions are part of a class, not a part of an object. If you want to call a public static member function outside the class, use the class name and the domain operator "alias ". For example

 

Box volume ();

 

In fact, static member functions can also be called by object names, such

 

A. Volume ();

 

But this does not mean that this function belongs to object A, but only uses the type.

 

Static member functions are used to process static data members.

 

It can be said that the fundamental difference between a static member function and a non-static member function is that a non-static member function has the this pointer. A static member function does not belong to a certain object and has nothing to do with any object, the static member function does not have the this pointer. It is determined that the static member function cannot access non-static members in this class.

 

In C ++ programs, static member functions are mainly used to access static data members rather than non-static members. Assume that a static member function has the following statements:

 

Cout

 

Cout <width <Endl; // If width is a non-static data member, it is invalid

 

However, it is not absolutely not allowed to reference non-static members in this class, but it is not allowed to perform default access because it cannot know which object to look. If you must reference non-static members of this class, you should add the object name and the member operator ".". For example

 

Cout <A. width <Endl; // reference non-static members in the Class Object.

 

Assume that a has been defined as a box object and is valid in the current scope, this statement is legal.

Static member 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.