Get the App
SLTechnology News&Howtos  ›  Development  › 

How to apply C++ friend and operator overloading

Shulou Source: shulou.com Published: 2022-05-31 19:51:03 10月03日 Update

This article mainly explains "how to apply C++ friends and operator overloading". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how C++ friends and operators overload.

Friendly yuan

In life, your home has a living room (Public) and your bedroom (Private). All guests in the living room can enter, but your bedroom is private, that is to say, only you can enter, but you can also allow your best friend and gay friend to enter.

In the program, some private properties also want to be accessed by some special functions or classes outside the class, so they need to use friend technology.

The purpose of friends is to allow one function or class to access private members of another class.

The keyword of friend is: friend

There are three implementations of friends:

Global function as friend

Class as friend

Member functions as friends

1 the global function friend class Building {/ / tells the compiler that the global function is a good friend of the Building class and can access the private content friend void goodGay (Building * building) in the class; public: Building () {this- > m_SittingRoom = "living room"; this- > m_BedRoom = "bedroom";} public: string m_SittingRoom / / living room private: string masked BedRoom; / / bedroom}; void goodGay (Building * building) {cout

Tags: Functions operations operators bedroom living room active member copy data private content type global age compiler compilation Clearing application function property Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Tech Info OPPO Reno Xiaomi