// C++
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string _name;
// Person의 private에 선언된 데이터에 Person2가 접근 가능.
friend class Person2;
public:
Person() : _name("liz")
{
}
};
class Person2
{
public:
void showName() const
{
Person p;
cout << p._name << endl; // 접근 가능.
}
};
int main()
{
Person2 p2;
p2.showName();
}
Output
liz
// C++
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string _name;
// 아래 프로토타입 모양의 함수는 접근 제어자에 영향을 받지 않는다.
friend void showName();
public:
Person() : _name("liz")
{
}
};
void showName()
{
Person p;
cout << p._name << endl;
}
int main()
{
showName();
}
Output
liz