C++ 프렌드

shinyeongwoon·2022년 11월 23일
0

C++

목록 보기
8/10

프렌드 함수

특정 클래스의 private 멤버에 접근할 수 있는 함수
프렌드 함수 선언은 클래스 내보에 선언, 함수 정의는 클래스에 속한 함수가 아니기 때문에 외부에 정의

  • friend keyword로 클래스 내에 선언된 함수
    클래스의 모든 멤버를 접근할 수 있는 권한 부여
    프렌드 함수라고 부름
  • 클래스의 멤버 함수가 아닌 외부 함수
    전역 함수
    다른 클래스의 멤버 함수
  • 프렌드 선언의 필요성
    클래스의 멤버로 선언하기에는 무리가 있고, 클래스의 모든 멤버를 자유롭게 접근할 수 있는 일부 외부 함수 작성 시

  • 프렌드 함수가 되는 3가지
    전역 함수 : 클래스 외부에 선언된 전역 함수
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;
}

0개의 댓글