包括:++运算符、+运算符、赋值运算符=、转换运算符、int() char*()运算符、==运算符。
1.重载++运算符
class TestSample
{
public:
TestSample()
{
};
~TestSample()
{
};
void SetNum(int iNum)
{
num = iNum;
}
int GetNum() const
{
return num;
}
const TestSample& operator ++ ()//前缀
{
++num;
return *this;
}
const TestSample& operator ++ (int)//后缀 注意: int根本没有使用,只是区分前缀后缀
{
TestSample testSample(*this);
++num;
return testSample;
}
private:
int num = 5;
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample testSimpleOne;
cout << "testSimpleOne:" << testSimpleOne.GetNum() << endl;
//++testSimpleOne;
TestSample testSimpleTwo;
testSimpleOne++;
testSimpleTwo = testSimpleOne++;
cout << "testSimpleOne:" << testSimpleOne.GetNum() << endl;
cout << "testSimpleTwo:" << testSimpleTwo.GetNum() << endl;
testSimpleTwo = ++testSimpleOne;
cout << "testSimpleOne:" << testSimpleOne.GetNum() << endl;
cout << "testSimpleTwo:" << testSimpleTwo.GetNum() << endl;
}
输出:
testSimpleOne:5
testSimpleOne:7
testSimpleTwo:6
testSimpleOne:8
testSimpleTwo:8
请按任意键继续. . .
2.重载+运算符
class TestSample
{
public:
TestSample(int num)
{
this->num = num;
};
TestSample()
{
};
~TestSample()
{
};
void SetNum(int iNum)
{
num = iNum;
}
int GetNum() const
{
return num;
}
const TestSample& operator + (const TestSample& obj)
{
return TestSample(num + obj.GetNum());
}
private:
int num = 0;
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample sampleOne(23), sampleTwo(17), sampleThree;
sampleThree = sampleOne + sampleTwo;
cout << "sampleThree = sampleOne + sampleTwo:" << sampleThree.GetNum() << endl;
}
输出:
sampleThree = sampleOne + sampleTwo:40
请按任意键继续. . .
3.重载=运算符
C++提供默认的构造函数、析构函数和拷贝构造函数,还有第四个由编译器提供的成员函数:赋值运算符(=)
TestSample sampleOne(23), sampleTwo(17);
sampleOne = sampleTwo;
cout << "sampleOne:" << sampleOne.GetNum() << endl;
默认的赋值运算符将sampleTwo赋值给sampleOne,sampleOne的num也由23变为17了。
大部分情况下默认的赋值运算符能够对付,与默认拷贝构造函数一样(浅复制,深复制区别),当类中有堆内存成员变量时,导致两个对象指向同一个内存区域,释放内存时会出错,默认函数会造成错误。
class TestSample
{
public:
TestSample()
{
pStr = new char[200];
strncpy_s(pStr, 200, "123456asdf", 10);
};
~TestSample()
{
if (pStr)
{
delete[] pStr;
pStr = nullptr;
}
};
void SetStr(char* iStr)
{
strncpy_s(pStr, 200, iStr, strlen(iStr));
}
char* GetString() const
{
return pStr;
}
const TestSample& operator = (const TestSample& obj)
{
if (this == &obj) //防止obj = obj的错误情况
return *this;
if (pStr)//释放*this已经在构造函数中分配的内存,避免内存泄露,无法释放,浪费内存空间
{
delete[] pStr;
pStr = nullptr;
}
pStr = new char[200];//重新分配内存
strncpy_s(pStr, 200, obj.pStr, strlen(obj.pStr));
return *this;
}
private:
char* pStr = nullptr;
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample sampleOne, sampleTwo;
sampleOne.SetStr("sampleOne!");
sampleTwo = sampleOne;
cout << "sampleTwo:" << sampleTwo.GetString() << endl;
}
输出:
sampleTwo:sampleOne!
请按任意键继续. . .
4.转换运算符
如何将一个内置类型值赋值给用户自定义的类对象?比如:将int赋值给类对象。
下面就实践转换运算符,它是一个构造函数的重载版本。
class TestSample
{
public:
TestSample()
{
};
TestSample(int newValue)//转换运算符,它是一个构造函数的重载版本
{
num = newValue;
};
~TestSample()
{
};
void SetNum(int num)
{
this->num = num;
}
int GetNum()
{
return this->num;
}
private:
int num = 0;
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample sample;
int beta = 12;
sample = beta;
cout <<"sample.GetNum() :" <<sample.GetNum() << endl;
}
输出:
sample.GetNum() :12
请按任意键继续. . .
5.int() char*()运算符
如何逆向4中讲到的呢,将用户自定义类对象赋值给int,char*?
class TestSample
{
public:
TestSample()
{
};
~TestSample()
{
};
operator unsigned int()//int()运算符
{
return num;
}
operator char*()//char*()运算符
{
return pstr;
}
void SetNum(int num)
{
this->num = num;
}
int GetNum()
{
return this->num;
}
private:
int num = 0;
char* pstr = "123afg";
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample sample;
sample.SetNum(15);
int alpha = sample;
char* beta = sample;
cout << "int alpha:" << alpha << endl;
cout << "char* beta:" << beta << endl;
}
输出:
int alpha:15
char* beta:123afg
请按任意键继续. . .
6.重载==运算符
如何比较两个对象是否相等?
class TestSample
{
public:
TestSample()
{
};
TestSample(int num)
{
this->num = num;
};
~TestSample()
{
};
bool operator == (const TestSample& obj)
{
return (this->GetNum() == obj.GetNum()) ? true : false;
}
void SetNum(int num)
{
this->num = num;
}
int GetNum() const
{
return this->num;
}
private:
int num = 0;
};
int _tmain(int argc, _TCHAR* argv[])
{
TestSample sampleone(23), sampletwo(23);
if (sampleone == sampletwo)//相等
cout << "sampletwo is equal to sampleone!" << endl;
sampleone.SetNum(21);
if (!(sampleone == sampletwo))//不等
cout << "sampletwo is not equal to sampleone!" << endl;
}
输出:
sampletwo is equal to sampleone!
sampletwo is not equal to sampleone!
请按任意键继续. . .