C++ Advanced Series Catalog
Use of C++ operator keywords (overloaded operators, functors, type conversion operators)
C++11 practical technology (1) the use of auto and decltype
C++11 practical technology (2) std::function and bind binder
C++11 Practical Technology (3) std::future, std::promise, std::packaged_task, async
Table of contents
definition
In C++11, operator is a keyword used to overload operators. By overloading operators, you can define how objects of your custom type behave when using built-in operators.
Operator overloading usage can generally be divided into the following three categories:
- operator can overload our operator "±*/=", etc.;
- Also overload our function call operator "operator()";
- You can also do type conversion operators
operator overloaded operator
Common usage:
#include <iostream>
class MyNumber {
private:
int value;
public:
MyNumber(int num) : value(num) {
}
int getValue() const {
return value;
}
// 重载加法运算符 '+'
MyNumber operator+(const MyNumber& other) {
int sum = value + other.value;
return MyNumber(sum);
}
};
int main() {
MyNumber num1(5);
MyNumber num2(10);
MyNumber sum = num1 + num2; // 使用重载的加法运算符进行相加操作
std::cout << "Sum: " << sum.getValue() << std::endl;
return 0;
}
You can see that MyNumber sum = num1 + num2; it seems to be the addition of objects, but it actually enters the overloaded + operator, and what is added is the member variable in the object.
operator overloaded function call operator
class Adder {
public:
int operator()(int a, int b) {
return a + b;
}
};
// 使用函数对象进行加法运算
Adder add;
int result = add(3, 4); // 调用 operator(),返回结果 7
It seems that add is the same as a function, but it is not. After overloading the function call operator, the instantiated add object can be used like a function. This is also known as a functor (this word is very vivid).
operator type conversion operator
A type conversion operator is a special class member function that defines conversions that transform values of a class type into values of other types. Conversion operators are declared within the body of the class definition, followed by the reserved word operator followed by the target type of the conversion.
class MyType {
public:
using fr_t = void(*)(int);
static void func(int a)
{
std::cout << "the value:" << a << std::endl;
}
operator fr_t() {
// 执行适当的转换操作
// 将 MyType 转换为 fr_t
return func;//这里是将函数指针赋给了fr_t
}
};
// 使用类型转换运算符进行类型转换
MyType obj;
obj(2); // 这里涉及到两步动作:一是调用 operator fr_t(),将 obj 转换为 fr_t 类型;二是调用了fr_t(2)
Principle: The conversion function must be a member function, the return type cannot be specified, and the formal parameter table must be empty; the return value is implicit, and the return value is the same as the converted type, that is, the function pointer type fr_t in the above prototype, And the returned function func must correspond;
obj(2); The execution process of this line of code is a bit difficult to understand, it can be divided into two steps:
- One is to call operator fr_t() to convert obj to fr_t type;
- The second is to call fr_t(2); here the fr_t function pointer has pointed to func, so it can be called directly.