컴퓨터 언어/C++

[C++] C++ 21일차 friend 변수와 friend 클래스

훈츠 2020. 4. 25. 09:20
반응형

 

안녕하세요. 훈츠입니다. 금일은 friend 변수와 friend 클래스에 관해 포스팅 합니다.

 

클래스 안에 friend 변수(friend variable in the class)

 

  • 일반 함수가 클래스의 모든 멤버 ( private , public ) 에 접근을 할수 있습니다.
  • friend 는 상속되지 않습니다.
  • 클래스 안에 멤버 함수를 두지 않고 외부에 두고자 하는경우에 사용 합니다.

 

형식 ( friend 함수의 형식 )

  • 클래스안에 함수 앞에 붙이고, 함수는 원형만 선언
    • friend bool IsEqual ( A & a, B & b ) ; // 클래스 내부
    • Bool IsEqual ( A & a, B & b ) { if …. // 클래스 외부

 

예제1 코드  (A 클래스 안에 friend 함수 선언)

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
 
class A {
private : 
int n_mVar;
public:
A(int var) :n_mVar(var) {}
friend bool IsEqual(A& a, A & b); // friend 함수 선언 
};
// friend 함수 A 클래스의 private 속성의 변수를 사용함. 
bool IsEqual(A& a, A& b) {
if (a.n_mVar == b.n_mVar) {
cout << "값이 같습니다." << endl;
return true;
}
else {
cout << "값이 다릅니다." << endl;
return false;
}
}
int main()
{
A test1(34), test2(12);
IsEqual(test1, test2);
return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

 

A 클래스 내부에 Private 속성을 비교하는것을 보실수 있는데요. A 클래스안에 있는 friend 를 -> static, public 으로 바꾸어 보면 아래 IsEqual 함수가 빨간색으로 접근 불가하다는 것을 확인해 볼수 있습니다.


예제2 코드 (A 클래스 , B 클래스 서로 다른 클래스에 friend 함수 선언)

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
32
33
34
35
class B;
class A {
private : 
int n_mVar;
public:
A(int var) :n_mVar(var) {}
friend bool IsEqual(A& a, B & b); // friend 함수 선언 
};
class B {
private:
int n_mVar;
public:
B(int var) :n_mVar(var) {}
friend bool IsEqual(A& a, B& b); // friend 함수 선언 
};
// friend 함수 A,B 클래스의 private 속성의 변수를 사용함. 
bool IsEqual(A& a, B& b) {
if (a.n_mVar == b.n_mVar) {
cout << "값이 같습니다." << endl;
return true;
}
else {
cout << "값이 다릅니다." << endl;
return false;
}
}
int main()
{
A test1(34);
B test2(12);
IsEqual(test1, test2);
return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

A 클래스 내에 friend 함수에 B클래스가 있기 때문에, B 클래스가 있다는것을 알려준 첫번째 라인을 한번 확인하십시요. 그리고 A 클래스와 B클래스 모두 friend 함수가 있어서 각각의 내부 private 값을 비교 할수 있었습니다.

 

friend 클래스 (friend class)

  • Friend 클래스로 선언된 클래스는 자신이 속한 클래스 안의 모든 멤버에 대한 접근 권한이 있습니다.
  • Private 역시 상관없이 멤버를 사용할 수 있다.

형식 ( friend 클래스 형식 )

  • 클래스앞에 friend를 붙인다.
  • friend class 클래스명;

 

예제1 코드 (A 클래스 내부에만 friend class B 선언)

 

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
32
33
34
35
36
class B;
class A {
private : 
int n_mVar;
public:
A(int var) :n_mVar(var) {}
friend bool IsEqual(A& a, B & b); // friend 함수 선언 
};
class B {
private:
int n_mVar;
public:
B(int var) :n_mVar(var) {}
friend bool IsEqual(A& a, B& b); // friend 함수 선언 
};
// friend 함수 A,B 클래스의 private 속성의 변수를 사용함. 
bool IsEqual(A& a, B& b) {
if (a.n_mVar == b.n_mVar) {
cout << "값이 같습니다." << endl;
return true;
}
else {
cout << "값이 다릅니다." << endl;
return false;
}
}
int main()
{
A test1(34);
B test2(12);
IsEqual(test1, test2);
return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 


 

예제2 코드 (A 클래스 내부에만 friend class B 선언)

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
class B;
class A {
private : 
int n_mVar;
friend class B;
public:
A() :n_mVar(12) {} 
void print(B& Ref);// { cout <<"B: "<< Ref.n_mVar << endl; }
};
class B {
private:
int n_mVar;
friend class A;
public:
B() :n_mVar(20) {}
void print(A& Ref) { cout << "A: " << Ref.n_mVar << endl;  }
};
void A::print(B& Ref) {
{ cout <<"B: "<< Ref.n_mVar << endl; }
}
int main()
{
A test1; //내부 n_mVar = 12 로 초기화
B test2; //내부 n_mVar = 20 로 초기화
test2.print(test1); //B 에서 A의 n_mVar private 값 읽기 12
test1.print(test2); //A 에서 B의 n_mVar private 값 읽기 20
return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

A 클래스의 private 멤버변수를 B 클래스에서 접근하고, 반대로 B클래스의 private 멤버변수를 A클래스에서 접근한 결과를 보여 줍니다.

반응형