11. 연산자 오버로딩 2
출처: 윤성우 열혈강의 C++ 예제코드
11.1. 반드시 해야 하는 대입 연산자의 오버로딩
- 디폴트 대입 연산자의 문제점
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
char * name;
int age;
public:
Person(char * myname, int myage)
{
int len=strlen(myname)+1;
name=new char[len];
strcpy(name, myname);
age=myage;
}
void ShowPersonInfo() const
{
cout<<"이름: "<<name<<endl;
cout<<"나이: "<<age<<endl;
}
~Person()
{
delete []name;
cout<<"called destructor!"<<endl;
}
};
int main(void)
{
Person man1("Lee dong woo", 29);
Person man2("Yoon ji yul", 22);
man2=man1;
man1.ShowPersonInfo();
man2.ShowPersonInfo();
return 0;
}
이름: Lee dong woo
나이: 29
이름: Lee dong woo
나이: 29
called destructor!
- 디폴트 대입 연산자의 문제점 해결
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
char * name;
int age;
public:
Person(char * myname, int myage)
{
int len=strlen(myname)+1;
name=new char[len];
strcpy(name, myname);
age=myage;
}
Person& Person::operator=(const Person& ref)
{
delete []name;
int len=strlen(ref.name)+1;
name= new char[len];
strcpy(name, ref.name);
age=ref.age;
return *this;
}
void ShowPersonInfo() const
{
cout<<"이름: "<<name<<endl;
cout<<"나이: "<<age<<endl;
}
~Person()
{
delete []name;
cout<<"called destructor!"<<endl;
}
};
int main(void)
{
Person man1("Lee dong woo", 29);
Person man2("Yoon ji yul", 22);
man2=man1;
man1.ShowPersonInfo();
man2.ShowPersonInfo();
return 0;
}
이름: Lee dong woo
나이: 29
이름: Lee dong woo
나이: 29
called destructor!
called destructor!
- 유도 클래스에서 대입연산자 오버라이딩 시 기초 클래스 대입연산자 호출하기
#include <iostream>
using namespace std;
class First
{
private:
int num1, num2;
public:
First(int n1=0, int n2=0) : num1(n1), num2(n2)
{ }
void ShowData() { cout<<num1<<", "<<num2<<endl; }
First& operator=(const First&ref)
{
cout<<"First& operator=()"<<endl;
num1=ref.num1;
num2=ref.num2;
return *this;
}
};
class Second : public First
{
private:
int num3, num4;
public:
Second(int n1, int n2, int n3, int n4)
: First(n1, n2), num3(n3), num4(n4)
{ }
void ShowData()
{
First::ShowData();
cout<<num3<<", "<<num4<<endl;
}
Second& operator=(const Second &ref)
{
cout<<"Second& operator=()"<<endl;
First::operator=(ref);
num3=ref.num3;
num4=ref.num4;
return *this;
}
};
int main(void)
{
Second ssrc(111, 222, 333, 444);
Second scpy(0, 0, 0, 0);
scpy=ssrc;
scpy.ShowData();
return 0;
}
- 이니셜라이저를 이용한 성능향상(함수호출 줄이기)
#include <iostream>
using namespace std;
class AAA
{
private:
int num;
public:
AAA(int n=0): num(n)
{
cout<<"AAA(int n=0)"<<endl;
}
AAA(const AAA &ref): num(ref.num)
{
cout<<"AAA(const AAA & ref)"<<endl;
}
AAA & operator=(const AAA &ref)
{
num=ref.num;
cout<<"operator=(const AAA &ref)"<<endl;
return *this;
}
};
class BBB
{
private:
AAA mem;
public:
BBB(const AAA & ref)
: mem(ref)
{ }
};
class CCC
{
private:
AAA mem;
public:
CCC(const AAA & ref)
{
mem=ref;
}
};
int main(void)
{
AAA obj1(12);
cout<<"*********************"<<endl;
BBB obj2(obj1);
cout<<"*********************"<<endl;
CCC obj3(obj1);
return 0;
}
AAA(int n=0)
*********************
AAA(const AAA & ref)
*********************
AAA(int n=0)
operator=(const AAA &ref)
11.2. 배열의 인덱스 연산자 오버로딩
- const를 이용한 operator[] 오버로딩
#include <iostream>
#include <cstdlib>
using namespace std;
class BoundCheckIntArray
{
private:
int * arr;
int arrlen;
BoundCheckIntArray(const BoundCheckIntArray& arr) { }
BoundCheckIntArray& operator=(const BoundCheckIntArray& arr) { }
public:
BoundCheckIntArray(int len) :arrlen(len)
{
arr=new int[len];
}
int& operator[] (int idx)
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
int operator[] (int idx) const
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
int GetArrLen() const
{
return arrlen;
}
~BoundCheckIntArray()
{
delete []arr;
}
};
void ShowAllData(const BoundCheckIntArray& ref)
{
int len=ref.GetArrLen();
for(int idx=0; idx<len; idx++)
cout<<ref[idx]<<endl;
}
int main(void)
{
BoundCheckIntArray arr(5);
for(int i=0; i<5; i++)
arr[i]=(i+1)*11;
ShowAllData(arr);
return 0;
}
- 객체저장용 배열클래스 만들기
#include <iostream>
#include <cstdlib>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y) { }
friend ostream& operator<<(ostream& os, const Point& pos);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
class BoundCheckPointArray
{
private:
Point * arr;
int arrlen;
BoundCheckPointArray(const BoundCheckPointArray& arr) { }
BoundCheckPointArray& operator=(const BoundCheckPointArray& arr) { }
public:
BoundCheckPointArray(int len) :arrlen(len)
{
arr=new Point[len];
}
Point& operator[] (int idx)
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
Point operator[] (int idx) const
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
int GetArrLen() const
{
return arrlen;
}
~BoundCheckPointArray()
{
delete []arr;
}
};
int main(void)
{
BoundCheckPointArray arr(3);
arr[0]=Point(3, 4);
arr[1]=Point(5, 6);
arr[2]=Point(7, 8);
for(int i=0; i<arr.GetArrLen(); i++)
cout<<arr[i];
return 0;
}
- 포인터저장용 배열클래스 만들기
#include <iostream>
#include <cstdlib>
using namespace std;
class Point
{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0) : xpos(x), ypos(y) { }
friend ostream& operator<<(ostream& os, const Point& pos);
};
ostream& operator<<(ostream& os, const Point& pos)
{
os<<'['<<pos.xpos<<", "<<pos.ypos<<']'<<endl;
return os;
}
typedef Point * POINT_PTR;
class BoundCheckPointPtrArray
{
private:
POINT_PTR * arr;
int arrlen;
BoundCheckPointPtrArray(const BoundCheckPointPtrArray& arr) { }
BoundCheckPointPtrArray& operator=(const BoundCheckPointPtrArray& arr) { }
public:
BoundCheckPointPtrArray(int len) :arrlen(len)
{
arr=new POINT_PTR[len];
}
POINT_PTR& operator[] (int idx)
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
POINT_PTR operator[] (int idx) const
{
if(idx<0 || idx>=arrlen)
{
cout<<"Array index out of bound exception"<<endl;
exit(1);
}
return arr[idx];
}
int GetArrLen() const
{
return arrlen;
}
~BoundCheckPointPtrArray()
{
delete []arr;
}
};
int main(void)
{
BoundCheckPointPtrArray arr(3);
arr[0]=new Point(3, 4);
arr[1]=new Point(5, 6);
arr[2]=new Point(7, 8);
for(int i=0; i<arr.GetArrLen(); i++)
cout<<*(arr[i]);
delete arr[0];
delete arr[1];
delete arr[2];
return 0;
}
11.3. 그 이외의 연산자 오버로딩
...