C ++ static members and friends

Source: Internet
Author: User

 

A static member is a member of a declared static class. All objects in the class share data. Youyuan is a common function defined outside the class, but it needs to be explained inside the class. In order to distinguish it from the member function of the class, add the keyword friend before the description. Youyuan is not a member function, but it can be a private member in the member class. Its Sharing improves program running efficiency. The friendly functions in the C ++ language provide convenience for Private Members in the out-of-class classes and protection for members, but damage the encapsulation and concealment of classes. Youyuan can be a function called youyuan function or a class called youyuan class. Youyuan functions and youyuan classes are collectively referred to as youyuan.

Static Member

A class is equivalent to a data type. When an object of a certain type is described, the system allocates a memory unit for the object to store all the members of the class. However, in some applications, all objects belonging to a class in the program need to share a data. To this end, a solution is to describe the data to be shared as a global variable, but this will undermine Data encapsulation; A better solution is to describe the data to be shared as a static member of the class.

Static Data Member

In C ++, when multiple objects are defined in the same class, each object has its own data member (excluding static data members ), all objects share one member function and one static data member. Static data members are shared among all objects of the class, rather than members of an object. Therefore, data sharing between multiple objects can be realized. Static data members do not belong to any object. They are not generated because of the creation of the object, nor are they deleted because of the object's structure. They are part of the class definition, therefore, using static data members will not damage the concealment of the class. Similar to common data members, static data members are defined as private static data members that cannot be accessed by the outside world. static data members can be accessed by functions with any access permission. You can change static data members in the member functions of the class.

Static data members do not belong to any specific object, so they must be initialized and cannot be initialized in constructors. Static data members are called static data members. The method used to describe a static data member is the same as that used to describe a common static variable, except that the former is described in one. The usage of static data members is as follows:

1-The definition of static data members is similar to that of general data members, but static keywords must be added before.

2-The initialization of static data members is different from that of general data members. The initialization format of static data members is as follows:

<Type> <class name >:< static data member >=< value>;

3-use the format when referencing static data members:

<Class name >:: <static data member>

Class class1

{

Int;

Static int B;

} C1, C2;

Int class1:; B;

Class1 contains two data members, A and B. A is a common data member, and there is a copy of each data member in the object C1 and C2; B is a static data member, and the member in all class1 class objects is actually the same variable. The C ++ compiler stores static data members in the static storage area. All data in the storage area is shared by all objects of the class.

Static member functions

The island of the static member function is the same as that of other functions. Note that static member functions are not virtual functions. The static member function is similar to the static data member and also belongs to the class. The static member function is defined by adding the static keyword before the general function definition. The format of calling a static member function is as follows:

<Class name >:< static member function name> (<parameter table> );

Static member functions are similar to static data members and are associated with classes instead of objects. As long as the class exists, static member functions can be used, therefore, objects are not required to access static member functions. If an object is used to call a static member function, only its type is used.

Static member functions can only access static data members, static member functions, and functions and data other than classes. You cannot access non-static data members in the metadata class (because non-static data members make sense only when the object exists ). However, static data members and static member functions can be accessed by functions with any permission. Similar to common member functions, static member functions also have an access mechanism. Private Static member functions cannot be accessed by the outside world. The static member function does not have the this pointer. Therefore, the static member function can only directly invoke the static member in the class. To do so, you must use the object name or pointer to the object.

 

Youyuan

Youyuan provides a data sharing mechanism between member functions of different classes and between member functions of the class and general functions. By using youyuan, a common function or a member function in another class can be a private member or a protected member in the category. The correct use of youyuan can improve the program running efficiency, but it damages the encapsulation of classes and data concealment.

Youyuan Function

The method to define a friend function is to use the keyword friend in the class definition to describe the function. The format is as follows:

Friend <type> <youyuan Function Name> (<parameter table> );

The location indicated by the youyuan function can be in any part of the class, either in the public area or in the protected area. The meaning is exactly the same. The youyuan function is defined outside the class, it is generally put together with the member function definition of the class. You can directly access all the members of the class, but it is not a member function and can be called anywhere like a work function. The method of defining a member function is to define the function anywhere in the class, just like defining other functions, and add the keyword friend before it. Although the member function is defined in the class, it is not a member function. It can be a common function or a member function of other classes, access the private or protected member of this class through the object name in its function body.

Youyuan class

C ++ allows you to specify a class as a friend class of another class ). If a is a member class of B, all member functions of a can access all members of Class B like a member function. The definition format is as follows:

Class B

{

All member functions of friend Class A: // A are membership functions of B.

}

A friend relationship cannot be inherited. Assume that Class A is a friend of Class B, and class C is derived from Class B. If Class C does not explicitly use the following statement:

Friend Class;

So, although class A is a friend of Class B, this relationship will not be inherited to Class C. That is to say, Class C and Class A have no friend relationship, the member functions of Class A cannot directly expose the protected and Private Members of class C.

There is no such relationship as "friends of friends. Assume that Class A is a friend of Class B, and Class B is a friend of class C, that is, the member functions of Class B can be protected members and Private Members of class C, A member function of Class A can be a protected and private member of class B. However, a member function of Class A cannot directly access protected and Private Members of class C, that is to say, the relationship between friends and Yuan does not have the passing ability.

Youyuan application instance

Calculate the square sum of two numbers

# Include <iostream. h>

Class myclass

{

PRIVATE:

Int A, B, Max, min;

Public:

Myclass (int I, Int J): A (I), B (j)

{

Max = (A> B )? A: B;

Min = (a <B )? A: B;

}

Friend int result (myclass & X );

};

Int result (myclass & X)

{

Return = x. Max * X. max-x.min * X. min;

}

Void main ()

{

Myclass N (3, 4)

Cout <result (n) <Endl;

}

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.