1

i am a new programer in c++

and i have an abstract class A, and i implemented operators in it.

and now i have two classes B and C that extend A.

for some reason the compiler does not identify the operators.

is it because that operators are not inherrited? or is it because that i have a code bug?

here is the code:

#ifndef A_
#define A_
class A{
public:
 friend bool operator==(const A &a1,const A &a2);
}
#endif
inline bool operator==(const A& a1, const A& a2){
....
}

is it not meant to work on B==C and B==B and C==C? thanx

Matt

asked Sep 23, 2010 at 18:36
5
  • 1
    What exactly are you asking? Please provide the compiler error messages and more detail. Thanks! Commented Sep 23, 2010 at 18:46
  • 1
    Matt, please provide a minimal, self-contained example which exhibits the problem you're seeing, accompanied by an exact description of what you're expecting ("should compile fine") and what your seeing (exact compiler error messages). Commented Sep 23, 2010 at 18:50
  • This could also depend on the version of the C++ compiler you are using. A very old Borland C++ might get function argument lookup wrong for example. Please tell us the compiler and version you are using. Commented Sep 23, 2010 at 18:54
  • I prefer not to have base class comparison operators given public accessibility. When an object of type B is passed to a function that operates on objects of type A, there is no easy process for the function to differentiate between an instance of B or C. Very bad design when using generic programming. Many of my early programs failed because of this. Commented Sep 23, 2010 at 19:02
  • 1
    Do you have all that in your header file? Having that inline bool operator== outside the include guard looks a bit suspicious. Commented Sep 23, 2010 at 21:02

4 Answers 4

2

The program compiles and runs as expected, correctly calling the right operator when I try it:

class A {
public:
 friend bool operator==(const A &a1, const A &a2);
};
bool operator==(const A &a1, const A &a2) {
 return false;
}
class B : public A {};
class C : public A {};
int main()
{
 B b;
 C c;
 bool equals = b == c;
 return 0;
}
answered Sep 23, 2010 at 18:49
Sign up to request clarification or add additional context in comments.

Comments

1

Put the operator inside the class.

#ifndef A_
#define A_
class A
{
 ...
public:
 bool operator==(const A &other)
 {
 // your operator logic
 }
}
#endif

Optionally, you could make it virtual thus allowing you to override it in the derived classes.

answered Sep 23, 2010 at 18:49

2 Comments

The friend approach is also valid: see @Reinderien's answer.
@Richard - Yes, I didn't say it is not invalid. :)
1

My suggestion: don't overload comparison operators in base classes, but implement equivalent protected methods. This will prevent some hard to detect failures in your program.

Example:

class Base
{
 protected:
 bool equal_base(const Base& other) const
 { return member == other.member;}
 private:
 unsigned int member;
};
class B_Child : public Base
{
 public:
 bool operator==(const B_Child& other) const
 { return (member_b == other_member_b) && equal_base(other);}
 private:
 std::string member_b;
};
class C_Child : public Base
{
 public:
 bool operator==(const C_Child& other) const
 { return (member_c == other_member_c) && equal_base(other);}
 private:
 double member_c;
};

Also search the web for "C++ virtual equality operator".

answered Sep 23, 2010 at 19:10

Comments

1

How about:

class A
{
 public:
 bool operator==(A const& rhs) const
 {
 return this->isEqual(rhs);
 }
 private:
 virtual bool isEqual(A const& rhS)
 {
 return /* Your test here */
 }
};
class B: public A
{
 private:
 virtual bool isEqual(A const& rhS)
 {
 B& theRealRHS = dynamic_cast<B const&>(rhs); // Throws if it is not a B
 return /* Your test here */
 }
};
answered Sep 23, 2010 at 20:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.