C ++ friend (Friend keyword), class overloads operator overloading (operator keyword)

C ++, a friend of usage:

  1, the use of friend keyword in a class declaration

  2, the class may be a friend of another class or specific functions

  3, part of the class is not a friend

  4, a friend from class level of access restrictions

  5, friends of direct access to all members of a specific class.

 

Friend declaration using the format:  

class class_name()
    {
        int x;
        int y;
        public:
            friend void func(class_name& p);
    };
    void func(class_name& p)
    {
        
    }

Friendship is not available transitive

Like a friend can be a member function of another class

Friend class can be a full class - all the member functions are friend

 

 

 

Summary friend:

  Friend for compatibility c language efficiency born.

  Friend of object-oriented direct damage encapsulation

  Friendship is not available transitive

  Like a friend can be a member function of another class

  Like a friend can be a complete class  

 

 

Class function overloading:

  1, the nature of overloaded functions are independent different functions

  2, C ++ function call is determined by the function name, function parameters

  3, can not get direct entry address of an overloaded function by function name

  4, only the same overloaded function scope

 

The importance of function overloading:

  Prompt on functional capabilities by function name.

  Prompt use of the function parameter list

  Expansion of the system already exists in the function function

 

 

C ++ overload operators (operator):

  In C ++ overload operators can extend functionality.

  Overload operator so as to perform a function.

  Nature:

  Extended operator functions with a special function of the form.

operator can define a special function key, the function overloaded operators.

class Type

{

  Type operator sign (const Type &t, const Type & t2)

  {

    Type right;

    Return the right;

  }    

}

Guess you like

Origin www.cnblogs.com/hjxzjp/p/11681786.html