C++에서 friend 함수 다루기
1. friend 함수의 개념
C, C++은 하드웨어 제어 등 가장 많이 쓰는 언어 중 하나이다. 이번에는 C++에서 friend 함수에 대해 파악해 본다. C++에서는 C에는 없는 friend 함수가 사용된다. 왜 friend 함수를 사용하는가? friend 키워드를 정의하면 class의 멤버 함수가 아니라 class 내부에서도 class 외부에서 선언하는 독립된 일반 함수로 동작한다. 따라서 class의 모든 멤버 변수(public, private, protected로 선언된 비공개 멤버변수 포함)에 접근하여 참조할 수 있는 전역함수로 사용할 수 있기 때문이다.
2. friend 함수 형식
friend 함수형 friend 멤버 함수명(매개변수);
3. friend 함수를 사용하는 방법
(예제 1)
//friend 함수 선언
class Sky
{
public:
friend void DataShow(Sky &obj); // friend 함수 선언. 전역 함수로서 어떤 class에도 속하지 않는 독립된 함수
private:
unsigned int Temperature ;
unsigned int Weather;
};
// friend 함수 정의
void DataShow(Sky &obj)
{
cout << “날씨:” << obj.Weather << endl; // friend 함수로서 어떤 class에도 속하지 않기 때문에 범위 접근 연산자(::)가 필요 없다.
}
4. friend 함수 예제
다음의 (예제 2)는 Seoul이라는 class를 정의하고 온도, 날씨를 입력한다. 그리고 여러 개의 class에서 공통 friend 함수를 사용할 때는 friend 함수를 사용하기 전 앞 부분에 모든 class의 이름을 가선언하여 class에 대한 기본 정보를 알려준다. 이것을 전방참조라고 한다. 이러면 각 class의 내부에 있는 friend 함수를 컴파일할 때 참조하여 에러 없이 컴파일 된다.
(예제 2)
#include <iostream>
using namespace std;
class Paris; // 전방 참조
class Seoul
{
public:
Seoul(int temp, int population) {Temp=temp; Population=population;}
friend void dataShow(Seoul &obj);
friend void cpy(Seoul &obj, Paris obj2); // 공통으로 있는 friend 함수
private:
friend class Paris; // friend class 선언. 아래 위치에 class Paris가 오므로
unsigned int Temp;
unsigned int Population;
};
class Paris
{
public:
Paris(int temp, int population) {Temp=temp; Population=population;}
friend void dataShow(Seoul &obj);
friend void cpy(Seoul &obj, Paris obj2);
private:
unsigned int Temp;
unsigned int Population;
};
void dataShow(Seoul &obj)
{
cout << "온도 :" << obj.Temp << endl;
cout << "인구 :" << obj.Population << endl;
}
void cpy(Seoul &obj, Paris obj2)
{
obj.Temp = obj2.Temp;
obj.Population = obj2.Population;
}
int main()
{
Seoul happy = Seoul(30, 1500);
Paris paris = Paris(25, 1000);
dataShow(happy);
cpy(happy, paris);
dataShow(happy);
return 0;
}
<그림1> (예제 2)의 실행 결과
5. friend 함수 응용
가. 다른 class의 함수를 friend 함수로 사용
(예제 3)
#include <iostream>
using namespace std;
class Seoul; // 전방 참조
class Paris
{
public:
Paris(int temp, int population) {Temp=temp; Population=population;}
void dataShow(Seoul &obj);
private:
unsigned int Temp;
unsigned int Population;
};
class Seoul
{
public:
Seoul(int temp, int population) {Temp=temp; Population=population;}
friend void Paris::dataShow(Seoul &obj); // Paris class에 있는 멤버함수를 friend 하여 여기서도 사용
private:
friend class Paris;
unsigned int Temp;
unsigned int Population;
};
void Paris::dataShow(Seoul &obj) //다른 class의 friend 함수이므로 범위 접근 연산자(::) 사용
{
cout << "온도 :" << obj.Temp << endl; //인자로 전달한 객체의 private 멤버를 참조
cout << "인구 :" << obj.Population << endl; //인자로 전달한 객체의 private 멤버를 참조
}
int main()
{
Seoul happy = Seoul(30, 1500);
Paris paris = Paris(25, 1000);
paris.dataShow(happy); // paris 객체의 멤버 호출. 멤버함수(다른 class의 객체)
return 0;
}
<그림2> (예제 3)의 실행 결과
나. friend class
friend 함수와 마찬가지로 friend class는 class 전체를 다른 특정한 class의 friend로 지정할 수 있다. 이렇게 하면 friend class로 선언한 class의 private 멤버변수에 접근하여 편리하게 사용할 수 있다.
(예제 4)
#include <iostream>
using namespace std;
class Seoul; // 전방 참조
class Paris
{
public:
Paris(int temp, int population) {Temp=temp; Population=population;}
void dataShow(Seoul &obj);
private:
unsigned int Temp;
unsigned int Population;
friend class Seoul; // Seoul class의 모든 멤버함수에서 Paris class의 private 멤버변수에 접근 할 수 있음.
};
class Seoul
{
public:
Seoul(int temp, int population) {Temp=temp; Population=population;}
void dataShow(Paris &obj);
private:
unsigned int Temp;
unsigned int Population;
};
void Seoul::dataShow(Paris &obj)
{
cout << "온도 :" << obj.Temp << endl;
cout << "인구 :" << obj.Population << endl;
}
int main()
{
Seoul happy = Seoul(30, 1500);
Paris paris = Paris(25, 1000);
happy.dataShow(paris);
return 0;
}
<그림3> (예제 4)의 실행 결과
6. 분석과 고찰
다른 class의 함수나 class를 접근 제한에 상관없이 프로그램의 원하는 곳에서 사용할 수 있다면 편리할 것이다. 이것을 가능하게 해 주는 것이 friend 함수와 friend class이다. 그런데 다른 class에서 friend 함수를 사용한다면 미리 자신의 class에서 다른 class에서 사용할 함수를 미리 만들어 놔야 한다는 것이 신경이 쓰인다. 예를 들면 (예제 3)에서 Seoul class에서 void dataShow(Seoul &obj) 함수를 사용한다면 미리 Paris class에서 void dataShow(Seoul &obj) 함수를 정의해 놔야 한다. 그런데 friend class에서는 다른 class를 불러올 때 friend class Seoul; 와 같이 코딩해도 다른 class를 사용할 수 있다.
<윤덕하 객원전문기자 (아이에셋)>
Copyright ⓒ 첨단 & Hellot.net