Friend Class allows friends to access their own private members.
The declaration of Virtual Class Member allows the program to access a member of the real object.
Class A declares that Class C is its friend class, so Class C can access the private member of Class A.
But the Friend relationship is not inherited, that is, Class D cannot access the private member of Class A. Is there any way to let the inheritance be inherited in this friend relationship?
The way is there!
If you don't want Class D to access the member variable of Class A, you can't make a text declaration in Class A (usually the API programming is like this, because it is impossible to know which class inherits Class C. or wants to make the program to become elegant Some, after all, in a class, I'm writing too many friend declarations that I don't feel very well), then write the Getxxx function in Class C and protect it protected. In this way, any derived class inherited from Class C can access the private class of Class A, of course, through the GET function.
code show as below:
#include
#include
Using namespace std;
Class A
{
Friend class b;
PUBLIC:
a (): _ a ("hello") {}
~ a () {}
Void Printa () {cout << _A << endl;}
Private:
String_A;
}
Class B
{
PUBLIC:
Void Bprinta (a * _a) {cout << _a -> _ a << endl;}
protected:
String & Geta (a * _a) {return_A -> _ a;
}
Class C: PUBLIC B
{
PUBLIC:
Void cseta (a * _a)
{
String & a_str = Geta (_A);
A_STR = "haha";
}
}
int main ()
{
a my_a;
MY_A.Printa (); // Output Hello
b my_b;
MY_B.BPRINTA (& MY_A); // Class B Access Class A
C my_c;
MY_C.cseta (& MY_A); // Class C also successfully accessed the private variable of Class A
MY_A.Printa (); // Output HAHA
Return 0;
}
The private member of Class A is accessible, but I want to access the private member of the derived class inherited from Class A?
Maybe you don't understand, maybe you think it is not practical. Ok, in order to prove that I am not discussing some technologies that have been used, I will combine a case.
I want to implement a problem with the event mechanism. Because because the life cycle of Event is controlled by the program, it must be generated with new, with Delete to release. And it is released after the OBServer is processed. In order to prevent programmers from accidentally drop Event, I set the Event's designer function to protected. In order to prevent programmers from generating objects, I set the constructor to protected to provide a static method crete () to generate an EVENT object and return a pointer. Then become the figure below:
However, the problem is coming, Observer wants the delete object Event, you must set Observer to Event's friend class.
But the real-generated EVENT object is its derived class, and the true handling of Event is not OBServer, but its derived class. However, according to the principle of Event-Observer design mode, the actual event does not have to care what it is Observer object. The solution is simple, set the designer function of Event to Virtual (in fact, it should be set to Virtual). The problem is solved. As shown below:
to sum up:
This shows two questions
1. Derived class can operate the private member of the friend object through the base class.
2. If the private method of the friend object is set to Virtual, the object of his friend relationship and its derived class can be a private method of his friend object.