Object-oriented C ++ static members and friends

Chapter XI static members and friends

11.1 Static member

1.C ++ class which static data members still borrow the reserved word static, but with the previous static global variables, static local variable and not related to static functions.

2. Static data members do not take up data space specific objects.

3. Create and destroy disposition static data members do not belong to any object. Objects have the right to read and write a static member. Static members should be able to access before creating anything in particular, it requires a static class members in the creation of any object

Just before the existence (like global variables, global objects executed right in front of the main function) and therefore should define static data members initialized before the main function.

4. static data members can not be defined not defined can not fail. Internal static data members of the class definition and on class initialization statements should be achieved among the source file.

The format is: type of data class name :: initialize static data member name = value;

Or: data type class name :: static data member name (initial value); (here is not to write a reserved word static)

5. lifetime of static data member is global, it does not matter whether the object is created.

6. When you want to access static data members, you can use object access and more generally with the class name to access.

Object name static data member name

Static data members of the class name :: Name

7. declaration syntax format static member function: static return type of a static member function name (formal parameter list) can not add the reserved word static in vitro class defined time!

8. The static member functions can be used by all such objects, it is noted that there is no implied passed static member function called "object address this" pointer to the this form of parameters, only objects explicitly passed as an argument.

 

11.2 Friend

1. Members of the Friends of the yuan is not a class.

2. friend into the friend category and friend functions.

3. friend functions on the increase in the class declaration in front of them express a friend function is not a member of this class, but this kind of friend functions.

4. friend function objects need to be explicitly passed to the function when the processing of the particular subject.

4. friend class: declare a class is a friend of another class, become the friend class. Friend class member functions all of which are in this kind of friend functions, are able to access all members of the class.

 

 

 

Guess you like

Origin www.cnblogs.com/joelovescoding/p/11829489.html