#include <time.h>
#include <opencv2\opencv.hpp>
#include <iostream>
#include <memory>
class TEST
{
public:
TEST() = default;
TEST(std::string s) : str(s)
{}
~TEST() = default;
std::string& operator + (std::string s)
{
this->str += "0000" + s;
return str;
}
std::string& operator ++ () // 對應 ++i
{
this->str += "78";
return str;
}
std::string& operator ++ (int) // 對應 i++
{
this->str += "78";
return str;
}
std::string getData()
{
return this->str;
}
private:
std::string str;
};
// #1 引用函數的參數,當然該參數也是一個引用
// #2 千萬不要返回局部對象的引用
std::ostream& operator << (std::ostream& out, TEST a) // 可以調用 cout<< 輸出
{
out << a.getData();
return out;
}
int main()
{
TEST a("good");
std::cout << (a++) << std::endl;
return 0;
}
代碼片段----重載操作符
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.