특정 클래스의 private 멤버에 접근할 수 있는 함수
프렌드 함수 선언은 클래스 내보에 선언, 함수 정의는 클래스에 속한 함수가 아니기 때문에 외부에 정의
class Rect{
...
friend bool equals(Rect r, Rect s);
}
bool equals(Rect r , Rect s){
....
}
다른 클래스의 멤버 함수 : 다른 클래스의 특정 멤버 함수
class Rect{
...
friend bool RectManager::equals(Rect r , Rect s){
}
}
class RectManager{
...
bool equals(iRect r , Rect s);
}
class RectManager::equals(Rect r , Rect s){
...
};
다른 클래스의 전체 : 다른 클래스의모든 멤버 함수
class Rect{
...
friend RectManager;
}
class RectManager{
...
bool equals(iRect r , Rect s);
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//전방 선언 : 실제 식별자 정의 전 컴파일러에게 존재를 미리 알리는 것
class Rect;
bool equals(Rect r, Rect s);
class Rect{
int width,height;
public:
Rect(int width, int height){ this-> width = width, this->height = height; }
// equals를 friend 함수로 정의
friend bool equals(Rect r, Rect s);
};
// 외부 전역 함수
bool equals(Rect r, Rect s){
if(r.width == s.width && r.height == s.height){
return true;
}
return false;
}
int main(){
Rect a(3,4), b(4,5);
if(equals(a,b)) cout << "equal" << endl;
else cout << "not equal" << endl;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//전방 선언 : 실제 식별자 정의 전 컴파일러에게 존재를 미리 알리는 것
class Rect;
class RectManager{
public:
bool equals(Rect r, Rect s);
};
class Rect{
int width,height;
public:
Rect(int width, int height){ this-> width = width, this->height = height; }
// RectManager equals를 friend 함수로 정의
friend bool RectManager::equals(Rect r, Rect s);
};
bool RectManager::equals(Rect r, Rect s){
if(r.width == s.width && r.height == s.height){
return true;
}
return false;
}
int main(){
Rect a(3,4), b(4,5);
RectManager man;
if(man.equals(a,b)) cout << "equal" << endl;
else cout << "not equal" << endl;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//전방 선언 : 실제 식별자 정의 전 컴파일러에게 존재를 미리 알리는 것
class Rect;
class RectManager{
public:
bool equals(Rect r, Rect s);
void copy(Rect& dest, Rect& src);
};
class Rect{
int width,height;
public:
Rect(int width, int height){ this-> width = width, this->height = height; }
// RectManager equals를 friend 함수로 정의
friend RectManager;
};
bool RectManager::equals(Rect r, Rect s){
if(r.width == s.width && r.height == s.height){
return true;
}
return false;
}
void RectManager::copy(Rect& dest,Rect &src){
dest.width = src.width;
dest.height = src.height;
}
int main(){
Rect a(3,4), b(4,5);
RectManager man;
man.copy(a,b);
if(man.equals(a,b)) cout << "equal" << endl;
else cout << "not equal" << endl;
}