Bob Hairgrove wrote:
Consider the following:
#include <string>
class A
{
public:
A( const std::string & full_name
, const std::string & display_name)
: m_full_name(full_name)
, m_display_name(display_name) {}
virtual ~A() {}
bool operator==(const A& other) const {
return (m_full_name == other.m_full_name)
&& (m_display_name == other.m_display_name);
}
private:
std::string m_full_name;
std::string m_display_name;
};
class B : public A
{
/* constructors etc. skipped ...*/
public:
bool operator==(const B& other) const {
return A::operator==(other);
}
private: /* etc. */
};
Is there an alternative syntax which can be used when calling the base
class operator== from the derived class (IOW leaving out the keyword
"operator")?
Thanks.
--
Bob Hairgrove
No**********@Home.com
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.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book