일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- react native
- 벡터와 리스트의 차이
- stencil buffer
- react native typescript navigate
- cyworld
- 스탠실 버퍼 튜토리얼
- react
- unity stencil buffer
- 싸이월드
- 리액트 네이티브 맥
- 리액트 네이티브 설치 오류
- node
- 스탠실 버퍼 시작
- react native ios 기기 연결
- c++ using
- C++
- react native typescript navigation
- react native typescript
- Expo
- GitHub
- react native accessible
- react-native
- react native 타입스크립트
- CSS
- react native mac
- html
- node.js
- c++ 정보은닉
- 스탠실 버퍼 사용
- javascript
Archives
- Today
- Total
혀니의 이거저거 뿌시기
[C++]06-2.클래스와 함수에 대한 friend 선언 본문
728x90
1)A클래스가 B클래스를 대상으로 friend선언을 하면, B클래스는 A클래스의 private멤버에 직접 접근 가능
2)단, A클래스도 B클래스의 private멤버에 직접 접근 가능하려면, B클래스가 A클래스를 대상으로 friend선언 해줘야함
class Boy
{
private:
int height; //키
friend class Girl; //Girl클래스를 friend로 선언함
public:
Boy(int len): height(len)
{}
. . . .
};
class Girl
{
private:
char phNum[20];
public:
Girl(char *num)
{
strcpy(phNum, num);
}
void ShowYourFriendInfo(Boy &frn)
{
cout <<"His height:" << frn.height << endl; //private함수에 접근
}
};
friend선언은 private, public상관 없음
<서로 선언 예제>
#include <iostream>
#include <cstring>
using namespace std;
class Girl; //Girl이라는 이름이 클래스의 이름임을 알림!
class Boy
{
private:
int height;
friend class Girl; //Girl클래스에 대한 friend 선언
public:
Boy(int len) : height(len)
{ }
void ShowYourFriendInfo(Girl &frn);
};
class Girl
{
private:
char phNum[20];
public:
Girl(char * num)
{
strcpy(phNum, num);
}
void ShowYourFriendInfo(Boy &frn);
friend class Boy; //Boy클래스에 대한 friend선언
};
void Boy::ShowYourFriendInfo(Girl &frn)
{
cout << "Her phone number: " <<frn.phNum << endl;
}
void Girl::ShowYourFriendInfo(Boy &frn)
{
cout << "His height: " <<frn.height << endl;
}
int main(void)
{
Boy boy(170);
Girl girl("010-1234-5678");
boy.ShowYourFriendInfo(girl);
girl.ShowYourFriendInfo(boy);
return 0;
}
friend선언은 언제?
정보은닉을 무너뜨리는 문법이므로 꼭 필요한 경우에만 사용해야 함,
함수의 friend 선언
전역함수를 대상으로도 클래스의 멤버함수를 대상으로도 friend선언 가능.
728x90
'LANG > C++' 카테고리의 다른 글
[C++]07-1.상속에 들어가기에 앞서 (0) | 2023.05.14 |
---|---|
[C++]06-3.C++에서의 static (1) | 2023.05.13 |
[C++]06-1.const와 관련해서 아직 못다한 이야기 (0) | 2023.05.13 |
[C++]05-3.복사 생성자의 호출시점 (0) | 2023.05.13 |
[C++]05-2.'깊은 복사'와 '얕은 복사' (0) | 2023.05.13 |