개발/C, C++

[C++] const, friend 키워드

huiyu 2018. 12. 30. 08:20

const

- 객체도 상수화 가능하며, 이는 const 멤버함수의 호출만 허용한다.
 -> 데이터를 변경시킬 능력이 있는 함수는 아예 호출을 허용하지 않는다.

  const SoSimple sim(20);


- const의 선언유무도 함수 오버로딩의 조건에 해당
  -> const로 선언된 객체는 const가 선언된 함수가 호출된다.

  void SimpleFunc() { ... }

  void SimpleFunc() const { .... }


 friend

 - 클래스를 대상으로 friend선언을 하게되면 선언한 클래스의 private 변수에 직접 접근이 가능하다.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstring>
 
using namespace std;
 
class Girl;
 
class Boy
{
private:
  int height;
  friend class Girl; //Girl class에 대한 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 Class에 대한 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;
}
cs

 -> Boy 클래스와 Girl 클래스는 서로 friend 선언을 하였고, ShowYourFriendInfo() 함수를 통해 직접 private변수에 접근하고 있다.


* 함수의 friend 선언
 -> 전역함수를 대상으로도, 클래스의 멤버함수를 대상으로도 friend 선언이 가능하다, friend선언된 함수는 자신이 선언된 클래스의 private 영역에 접근이 가능하다.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstring>
 
using namespace std;
 
class Point;
 
class PointOP
{
private:
  int opcnt;
public:
  PointOP() : opcnt(0)
  {}
  
  Point PointAdd(const Point&const Point&);
  Point PointSub(const Point&const Point&);
  ~PointOP()
  {
    cout<<"Operation times: "<<opcnt<<endl;
  }
};
 
class Point
{
private:
  int x;
  int y;
public:
  Point(const int &xpos, const int &ypos) : x(xpos), y(ypos)
  {}
  friend Point PointOP::PointAdd(const Point&const Point&);
  friend Point PointOP::PointSub(const Point&const Point&);
  friend void ShowPointPos(const Point&);
};
 
Point PointOP::PointAdd(const Point& pnt1, const Point& pnt2)
{
  opcnt++;
  return Point(pnt1.x+pnt2.x, pnt1.y+pnt2.y);
}
 
Point PointOP::PointSub(const Point& pnt1, const Point&pnt2)
{
  opcnt++;
  return Point(pnt1.x-pnt2.x, pnt1.y-pnt2.y);
}
 
int main(void)
{
  Point pos1(12);
  Point pos2(24);
  PointOP op;
 
  ShowPointPos(op.PointAdd(pos1, pos2));
  ShowPointPos(op.PointSub(pos2, pos1));
  return 0;
}
 
void ShowPointPos(const Point& pos)
{
  cout<<"x: "<<pos.x<<",";
  cout<<"y: "<<pos.y<<endl;
}
 
cs

 - 4행 : 16, 17행을 컴파일하기 위해서는 Point가 클래스의 이름임을 컴파일러에 알려줘야 한다. Point 클래스는 뒤에서 등장하기 때문에 이렇게 별도로 Point가 클래스의 이름임을 선언해야 한다.
 - 32, 33행 : PointOP 클래스의 멤버함수 PointAdd와 PointSub에 대해 friend 선언을 하고 있다.
 - 34행 : 60행에 정의된 함수 ShowPointPos에 대해 friend 선언
 - 40, 46행 : PointAdd와 PointSub함수는 Point 클래스의 friend로 선언되었기 때문에 private 멤버에 접근이 가능하다.
 - 62, 63행 : ShowPointPos함수도 Point클래스의 friend로 선언되었기 때문에 private멤버에 접근이 가능하다.

[참고자료]
윤성우의 열혈 C++프로그래밍 06-1 const와 관련해서 아직 못다한 이야기
06-2 클래스와 함수에 대한 friend 선언

728x90
반응형

'개발 > C, C++' 카테고리의 다른 글

[C++] 상속(Inheritance)  (0) 2018.12.31
[C++] C++에서의 static  (0) 2018.12.30
[C++] 복사 생성자의 호출시점  (1) 2018.12.29
[C++] 복사생성자의 깊은복사 & 얕은복사  (0) 2018.12.29
[C++] 복사 생성자(Copy Constructor)  (0) 2018.12.18