Excepto el operador de asignación, el resto de operadores son heredados automáticamente en la clase derivada. Esto se puede demostrar heredando de C12:Byte.h:
//: C14:OperatorInheritance.cpp // 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); } ///:~
Listado 14.16. C14/OperatorInheritance.cpp
El código de prueba anterior es idéntico a C12:ByteTest.cpp excepto que Byte2 se usa en vez de Byte. De esta forma todos los operadores son verificados para trabajar con Byte2 a través de la herencia.
Cuando examina la clase Byte2, verá que se ha definido explícitamente el constructor y que solo se ha credo el operator= que asigna un Byte2 a Byte2; cualquier otro operador de asignación tendrá que se realizado por usted.