
Same operator can define various function. It’s called “Operator Overloading”. First we need assign operator to resolve that assign between object.
let’s type “operator=()” instread =.
#include <iostream>
using namespace std;
class String
{
public:
String(char ch, int nSize);
~String();
void operator=(const String& s);
void SetData();
private:
int nLength;
char *pBuffer;
};
String::String(char ch, int nSize)
{
nLength = nSize;
pBuffer = new char[nLength + 1];
memset(pBuffer, ch, nLength);
pBuffer[nLength] = '\0';
}
String::~String()
{
delete[] pBuffer;
}
void String::operator=(const String& s)
{
delete pBuffer;
nLength = s.nLength;
pBuffer = new char[nLength + 1];
strcpy(pBuffer, s.pBuffer);
}
void String::SetData()
{
cout << "pBuffer: " << this->pBuffer << endl;
cout << "nLength: " << this->nLength << endl;
}
int main() {
String str1('A', 3), str2('B', 5);
cout << "before assign str2";
str2.SetData();
str2 = str1;
cout << "after assign str2";
str2.SetData();
return 0;
}
str2 = str1; is same mean str2.operator=(str1);// overloading object assignment operator
void String::operator=(const String& s)
{
delete pBuffer;
nLength = s.nLength;
pBuffer = new char[nLength + 1];
strcpy(pBuffer, s.pBuffer);
}
str1 = str1void String::operator=(const String& s)
{
if(&s == this)
return;
delete pBuffer;
nLength = s.nLength;
pBuffer = new char[nLength + 1];
strcpy(pBuffer, s.pBuffer);
}
How work 3 over object assignmnets like str1 = str2 = str3. it’s have to assigned str1 = (str2 = str3);
#include <iostream>
using namespace std;
class String
{
public:
String(char ch, int nSize);
~String();
String& operator=(const String& s);
void SetData();
private:
int nLength;
char *pBuffer;
};
String::String(char ch, int nSize)
{
nLength = nSize;
pBuffer = new char[nLength + 1];
memset(pBuffer, ch, nLength);
pBuffer[nLength] = '\0';
}
String::~String()
{
delete pBuffer;
}
String& String::operator=(const String& s)
{
if(&s == this)
return *this;
delete pBuffer;
nLength = s.nLength;
pBuffer = new char[nLength + 1];
strcpy(pBuffer, s.pBuffer);
return *this;
}
void String::SetData()
{
cout << endl;
cout << "pBuffer: " << this->pBuffer << endl;
cout << "nLength: " << this->nLength << endl;
}
int main() {
String str1('A', 3), str2('B', 3), str3('C', 4);
cout << "before assign str1";
str1.SetData();
str1 = str2 = str3;
cout << "after assign str1";
str1.SetData();
return 0;
}
return *this; on assign operatorQ2)
/*
String 클래스를 기반으로 한다.
MyString 클래스라는 이름으로 새로 생성한다.
기능은 String과 동일하다.
두 개의 객체 str1과 str2를 선언한다.
이 때 str1 생성 시 생성자의 전달인자는 (‘A’, 5),
str2 생성 시 생성자의 전달인자는 (‘Z’, 10) 이라고 하자.
이 때 str1에 str2를 대입한다. (str1 = str2) 그리고, 대입이 된 str1의 멤버변수를
화면에 출력하자. 대입 시 문제가 발생하였다면 대입 연산자 오버로딩을 통해 문제를
해결하도록 한다.
*/
#include <iostream>
using namespace std;
class MyString {
private:
char* ch;
int size;
public:
MyString(char c, int s) {
size = s;
ch = new char[size + 1];
memset(ch, c, size);
ch[size] = '\0';
cout << ch << endl;
}
~MyString() { delete[] ch; }
MyString& operator=(const MyString& s) {
if (&s == this) return *this;
delete ch;
size = s.size;
ch = new char[size + 1];
strcpy(ch, s.ch);
return *this;
}
};
int main() {
MyString str1('A', 5);
MyString str2('B', 10);
str1 = str2;
return 0;
}
📚 reference
- book - C++ 프로그래밍과 STL (이창현)
- Photo by Tachina Lee on Unsplash