除了赋值运算符以外,其余的运算符可以自动地继承到派生类中
//: C14:OperatorInheritance.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Inheriting overloaded operators
#include "../C12/Byte.h"
#include <fstream>
using namespace std;
ofstream out("ByteTest.out");
class Byte2 : public Byte {
public:
// Constructors don't inherit:
Byte2(unsigned char bb = 0) : Byte(bb) {}
// operator= does not inherit, but
// is synthesized for memberwise assignment.
// However, only the SameType = SameType
// operator= is synthesized, so you have to
// make the others explicitly:
Byte2& operator=(const Byte& right) {
Byte::operator=(right);
return *this;
}
Byte2& operator=(int i) {
Byte::operator=(i);
return *this;
}
};
// Similar test function as in C12:ByteTest.cpp:
void k(Byte2& b1, Byte2& b2) {
b1 = b1 * b2 + b2 % b1;
#define TRY2(OP) \
out << "b1 = "; b1.print(out); \
out << ", b2 = "; b2.print(out); \
out << "; b1 " #OP " b2 produces "; \
(b1 OP b2).print(out); \
out << endl;
b1 = 9; b2 = 47;
TRY2(+) TRY2(-) TRY2(*) TRY2(/)
TRY2(%) TRY2(^) TRY2(&) TRY2(|)
TRY2(<<) TRY2(>>) TRY2(+=) TRY2(-=)
TRY2(*=) TRY2(/=) TRY2(%=) TRY2(^=)
TRY2(&=) TRY2(|=) TRY2(>>=) TRY2(<<=)
TRY2(=) // Assignment operator
// Conditionals:
#define TRYC2(OP) \
out << "b1 = "; b1.print(out); \
out << ", b2 = "; b2.print(out); \
out << "; b1 " #OP " b2 produces "; \
out << (b1 OP b2); \
out << endl;
b1 = 9; b2 = 47;
TRYC2(<) TRYC2(>) TRYC2(==) TRYC2(!=) TRYC2(<=)
TRYC2(>=) TRYC2(&&) TRYC2(||)
// Chained assignment:
Byte2 b3 = 92;
b1 = b2 = b3;
}
int main() {
out << "member functions:" << endl;
Byte2 b1(47), b2(9);
k(b1, b2);
} ///:~
通过继承检测了所有运算符是否可以对Byte2进行操作
检测类Byte2时,必须显式定义构造函数,同时仅仅生成了可以把Byte2赋值
于Byte2类型的operator=,而任何我们需要的赋值运算符将由我们自己生成
输出 工程目录下 ByteTest.out
member functions:
b1 = 0x9, b2 = 0x2f; b1 + b2 produces 0x38
b1 = 0x9, b2 = 0x2f; b1 - b2 produces 0xda
b1 = 0x9, b2 = 0x2f; b1 * b2 produces 0xa7
b1 = 0x9, b2 = 0x2f; b1 / b2 produces 0x0
b1 = 0x9, b2 = 0x2f; b1 % b2 produces 0x9
b1 = 0x9, b2 = 0x2f; b1 ^ b2 produces 0x26
b1 = 0x9, b2 = 0x2f; b1 & b2 produces 0x9
b1 = 0x9, b2 = 0x2f; b1 | b2 produces 0x2f
b1 = 0x9, b2 = 0x2f; b1 << b2 produces 0x0
b1 = 0x9, b2 = 0x2f; b1 >> b2 produces 0x0
b1 = 0x9, b2 = 0x2f; b1 += b2 produces 0x38
b1 = 0x38, b2 = 0x2f; b1 -= b2 produces 0x9
b1 = 0x9, b2 = 0x2f; b1 *= b2 produces 0xa7
b1 = 0xa7, b2 = 0x2f; b1 /= b2 produces 0x3
b1 = 0x3, b2 = 0x2f; b1 %= b2 produces 0x3
b1 = 0x3, b2 = 0x2f; b1 ^= b2 produces 0x2c
b1 = 0x2c, b2 = 0x2f; b1 &= b2 produces 0x2c
b1 = 0x2c, b2 = 0x2f; b1 |= b2 produces 0x2f
b1 = 0x2f, b2 = 0x2f; b1 >>= b2 produces 0x0
b1 = 0x0, b2 = 0x2f; b1 <<= b2 produces 0x0
b1 = 0x0, b2 = 0x2f; b1 = b2 produces 0x2f
b1 = 0x9, b2 = 0x2f; b1 < b2 produces 1
b1 = 0x9, b2 = 0x2f; b1 > b2 produces 0
b1 = 0x9, b2 = 0x2f; b1 == b2 produces 0
b1 = 0x9, b2 = 0x2f; b1 != b2 produces 1
b1 = 0x9, b2 = 0x2f; b1 <= b2 produces 1
b1 = 0x9, b2 = 0x2f; b1 >= b2 produces 0
b1 = 0x9, b2 = 0x2f; b1 && b2 produces 1
b1 = 0x9, b2 = 0x2f; b1 || b2 produces 1