On Mon, 09 Aug 2004 15:25:45 GMT, Thomas Matthews
<Thomas_MatthewsSpitsOnSpamBots@sbcglobal.net> wrote:
[color=blue]
>Having wasted two weeks of debugging time due to
>this issue, I have devised the following:
>class Base
>{
> protected:
> bool equal_to(const Base& b) const;
>};
>
>class Derived
> : public Base
>{
> public:
> bool operator==(const Derived& d) const
> {
> bool result = Base::equal_to(d);
> // ...
> return result;
> }
>};
>
>The above idiom allows the compiler to check for
>instances where two variables of type Base are
>compared, or when two pointers to two different
>children are compared via parent pointers.
>class Monkey
> : public Base
>{
> public:
> bool operator==(const Monkey& m) const
> {
> bool result = Base::equal_to(d);
> // ...
> return result;
> }
>};
>
>// Code fragment:
> Base * p_monkey = new Monkey;
> Base * p_derived = new Derived;
> bool result;
>
> result = *p_monkey == p_derived;
>// End of code fragment.
>
>The assignment above is allowed with virtual
>operator==() methods in the base class. In
>my idiom, the compiler will flag the assignment
>because operator== is not defined in the base
>class.
>
>This is just something that I use, your mileage
>may vary.[/color]
It looks good!
I was just looking at this and another thread concerning virtual
operator=() (assignment op) and inheritance. In my example, operator==
is not virtual, and the derived operator== takes a different type,
therefore the base class operator== is not overridden ... merely
overloaded. Also, my base class is not abstract, so I would like to
have a "real" operator== in the base class.
I suppose all classes could call the protected "equal_to" function,
including the base class' operator==(). That would keep all
implementation code in one place. Do you see any drawbacks to this
method?
Thanks for your time and help!
--
Bob Hairgrove
NoSpamPlease@Home.com