在学习ZLToolKit源码时,发现代码中涉及好多运算符重载,因此对其做一下归类学习。
直接写一个代码案例如下:
#include <iostream>
#include <memory>
#include <functional>
// 定义类 A
class A {
public:
A(int a) { _a = a; }
// 重载()
std::string operator()(std::string s) {
std::cout << "-----operator () -----" << std::endl;
return s;
}
// 重载bool类型
operator bool() {
std::cout << "-----operator bool -----" << std::endl;
return _a > 0;
}
// 重载int类型
operator int() {
std::cout << "-----operator int -----" << std::endl;
return _a;
}
// 重载=运算法,入参(被赋值类型int)
A operator= (int a) {
std::cout << "-----operator =(int) -----" << std::endl;
_a = a;
return *this;
}
// 重载=运算法,入参(被赋值类型A)
A operator= (A a) {
std::cout &l

最低0.47元/天 解锁文章
919

被折叠的 条评论
为什么被折叠?



