自定义运算符实现(Programming Custom Implementations for Operators)

为运算符编写自定义实现是 C++ 允许自定义类作为“值类型”(value type)的机制,从而使其行为类似于内置的基本类型(如 int)。例如,用户代码期望能够执行如下操作:

int iX = iY + iZ + 9;

如果希望自定义类也能支持类似的操作方式,则需要定义运算符重载,例如:

const CRType CRType::operator+(const CRType& aroRhs) {body}
Last modified: Thursday, 30 January 2025, 12:33 PM