Home > Back-end >  A member function do friend yuan error,
A member function do friend yuan error,

Time:10-26

//member function do friends yuan
//I also declares the Show () as Per below a friend, but why still error can't access m_A,
# include & lt; iostream>
using namespace std;
The class Son;
The class Per
{
//declare friend yuan
Friend void Son: : Show ();
Public:
Per () {
M_A=111;
M_B=222;
}
Int m_B;
Private:
Int m_A;
};
The class Son
{
Public:
Per * p;
Son () {
P=new Per ();
}
Void the Show () {
//the error still cannot access private members
//cout & lt; <"Access to the private members=" & lt;

}
Void Show1 () {
Cout & lt; <"Access to public members=" & lt;

}
};

Int main ()
{
Son s;
S.S how ();
S.S how1 ();
return 0;
}

CodePudding user response:

A big move

# include & lt; iostream>
using namespace std;
The class Son;
The class Per;

The class Son
{
Public:
Per * p;
Son ();
Void the Show ();
Void Show1 ();
};
The class Per
{
//declare friend yuan
Friend void Son: : Show ();
Public:
Per () {
M_A=111;
M_B=222;
}
Int m_B;
Private:
Int m_A;
};
Son: : Son () {
P=new Per ();
}
Void Son: : Show ()
{
//the error still cannot access private members
Cout & lt; <"Access to the private members=" & lt;

}
Void Son: : Show1 () {
Cout & lt; <"Access to public members=" & lt;

}
Int main ()
{
Son s;
S.S how ();
S.S how1 ();
return 0;
}