6. friend와 static 그리고 const
출처: 윤성우 열혈강의 C++ 예제코드
6.1. const와 관련해서 아직 못다한 이야기
- const를 이용한 overloading
#include <iostream>
using namespace std;
class SoSimple
{
private:
int num;
public:
SoSimple(int n) : num(n)
{ }
SoSimple& AddNum(int n)
{
num+=n;
return *this;
}
void SimpleFunc ()
{
cout<<"SimpleFunc: "<<num<<endl;
}
void SimpleFunc () const
{
cout<<"const SimpleFunc: "<<num<<endl;
}
};
void YourFunc(const SoSimple &obj)
{
obj.SimpleFunc();
}
int main(void)
{
SoSimple obj1(2);
const SoSimple obj2(7);
obj1.SimpleFunc();
obj2.SimpleFunc();
YourFunc(obj1);
YourFunc(obj2);
return 0;
}
SimpleFunc: 2
const SimpleFunc: 7
const SimpleFunc: 2
const SimpleFunc: 7
6.2. 클래스와 함수에 대한 friend 선언
- 클래스에 friend 선언하기(클래스의 private 멤버를 외부에서 접근)
#include <iostream>
#include <cstring>
using namespace std;
class Girl;
class Boy
{
private:
int height;
friend class Girl;
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;
};
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;
}
- 함수에 friend 선언하기(함수가 선언된 클래스의 private 멤버를 외부에서 접근)
#include <iostream>
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(1, 2);
Point pos2(2, 4);
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;
}
6.3. C++에서의 static
- static 멤버변수 사용하기(private)
#include <iostream>
using namespace std;
class SoSimple
{
private:
static int simObjCnt;
public:
SoSimple()
{
simObjCnt++;
cout<<simObjCnt<<"번째 SoSimple 객체"<<endl;
}
};
int SoSimple::simObjCnt=0;
class SoComplex
{
private:
static int cmxObjCnt;
public:
SoComplex()
{
cmxObjCnt++;
cout<<cmxObjCnt<<"번째 SoComplex 객체"<<endl;
}
SoComplex(SoComplex ©)
{
cmxObjCnt++;
cout<<cmxObjCnt<<"번째 SoComplex 객체"<<endl;
}
};
int SoComplex::cmxObjCnt=0;
int main(void)
{
SoSimple sim1;
SoSimple sim2;
SoComplex cmx1;
SoComplex cmx2=cmx1;
SoComplex();
return 0;
}
- static 멤버변수 사용하기(public)
#include <iostream>
using namespace std;
class SoSimple
{
public:
static int simObjCnt;
public:
SoSimple()
{
simObjCnt++;
}
};
int SoSimple::simObjCnt=0;
int main(void)
{
cout<<SoSimple::simObjCnt<<"번째 SoSimple 객체"<<endl;
SoSimple sim1;
SoSimple sim2;
cout<<SoSimple::simObjCnt<<"번째 SoSimple 객체"<<endl;
cout<<sim1.simObjCnt<<"번째 SoSimple 객체"<<endl;
cout<<sim2.simObjCnt<<"번째 SoSimple 객체"<<endl;
return 0;
}
- static 상수 사용하기
#include <iostream>
using namespace std;
class CountryArea
{
public:
const static int RUSSIA =1707540;
const static int CANADA =998467;
const static int CHINA =957290;
const static int SOUTH_KOREA =9922;
};
int main(void)
{
cout<<"러시아 면적: "<<CountryArea::RUSSIA<<"㎢"<<endl;
cout<<"캐나다 면적: "<<CountryArea::CANADA<<"㎢"<<endl;
cout<<"중국 면적: "<<CountryArea::CHINA<<"㎢"<<endl;
cout<<"한국 면적: "<<CountryArea::SOUTH_KOREA<<"㎢"<<endl;
return 0;
}
- mutable 키워드를 이용하여 const함수에서 변수값 변경하기
#include <iostream>
using namespace std;
class SoSimple
{
private:
int num1;
mutable int num2;
public:
SoSimple(int n1, int n2)
: num1(n1), num2(n2)
{ }
void ShowSimpleData() const
{
cout<<num1<<", "<<num2<<endl;
}
void CopyToNum2() const
{
num2=num1;
}
};
int main(void)
{
SoSimple sm(1, 2);
sm.ShowSimpleData();
sm.CopyToNum2();
sm.ShowSimpleData();
return 0;
}