Hello,
I'm trying to implement the design below. I'd prefer to use commented
operator+() in class Number instead of the associated, uncommented
operator+(), but as I understand Covariant Return Types, one is not
allowed to return non-references (eg, references or pointers) in
virtual-functions (which seems to make sense given the seemingly
polymorphic nature).
So...I use the hack below and make my Object-return function be
non-virtual.
2 questions:
1) Is this "hack" appropriate so long as I manually make sure I
override all the non-virtual functions in my derived classes?
2) Can anyone suggest other ways to solve my problem?
I'll readily admit that I do not yet properly understand some of these
advanced C++ topics.
More details below.
Thanks for any help,
-Matt
A little background: I need two object/class types that share the
same methods, one of which is numerically "larger" then the other such
that the larger number can handle arithmetic overflows (without losing
the resulting larger number) of the smaller number. ie, if SmallNum
is one byte big, and BigNum is two bytes big, then:
(SmallNum) 0xFF + (SmallNum) 0x01 = (BigNum) 0x0100
class Number
{
public:
virtual Number& operator = (const Number &rval) = 0;
//virtual Number operator + (const Number &rval) = 0;
Number operator + (const Number &rval);
};
class BigNum : public Number
{
public:
BigNum() { };
BigNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};
class SmallNum : public Number
{
public:
SmallNum() { };
SmallNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};
int main() { BigNum bn; SmallNum sn; return 0; }
--
Remove the "downwithspammers-" text to email me.