Call the relationship between the static member function in C ++ static data members, non-static data members, non-static member function

C ++, these three types of relationships are as follows:

      We must first understand: static member function can call non-static data members, specifically how to call, see below.

class A 

{

public:

       static void fun (  A  a )

       { 

              cout << b <<endl;(错误)

              cout << a.b<<endl;(正确)

        }

privte:

       int b;

}

As in the example above, if the static member function to call non-static data member, you must be using an object specific call, why?

The reason: Because static member functions and non-static data members are owned for the entire class, not specific to an object-specific, to call the static member function it is not the purpose of the object, unlike non-static member function as there is a we know this is like implicit objects of this class to access non-static members. So static member function parameter must be a way to access an object of a specific non-static data members.

to sum up:

     (1) static member function can directly call the static data members and non-static member functions of the class, but call non-static data members when necessary to call the object parameter passed by the way.

     (2) In general, (1) is also not recommended to call this non-static data members, the main role is to access static member function static data members of a class, the maintenance of data sharing between objects.

Published 23 original articles · won praise 4 · Views 9981

Guess you like

Origin blog.csdn.net/hxp1994/article/details/89810109