Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling operators of ancestor classes

Status
Not open for further replies.

globos

Programmer
Nov 8, 2000
260
FR
Hi,

I have defined a virtual operator== in a class, and redefined it in its heirs.
In the heirs i want to call operator== of the super class.
But i don't know the syntax, how do we write it?

class A
{
public:
virtual bool operator== (const A& other) const
{
//do stuff
}
};

class B : public A
{
public:
bool operator== (const A& other) const
{
const B* b = dynamic_cast<const B*> (&other);
//do stuff
//How to call A::eek:perator== ?
}
};

--
Globos
 
As far as I know, opeartors are not inherited.
you can use them indirectly:
class A
{
protected:
xFunction(const A& other){....
public:

virtual bool operator== (const A& other) const
{
xFunction(A);
.....
}
};

class B : public A
{
public:
bool operator== (const A& other) const
{
const B* b = dynamic_cast<const B*> (&other);
A::xFunction(*b);
....

}
};


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
>I have defined a virtual operator

There is no such thing.



/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
>>I have defined a virtual operator

>There is no such thing.

See the example :

Code:
#include <iostream>

class A
{
public:
  A (int attr) : _attr (attr)
  {
  }
  int attr () const
  {
    return _attr;
  }
  virtual bool operator== (const A& other) const
  {
    std::cout << &quot;A operator==&quot; << std::endl;
    return _attr == other.attr ();
  }
protected:
  int _attr;
};

class B: public A
{
public:
  B (int attr) : A (attr)
  {
  }
  bool operator== (const A& other) const
  {
    std::cout << &quot;B operator==&quot; << std::endl;
    return _attr == other.attr ();
  }
};

int main ()
{
  A* b = new B (5);
  A* c = new B (5);

  if (*b == *c)
    std::cout << &quot;equals&quot; << std::endl;

  return 0;
}

Output of this program is :
B operator==
equals

It means that operator== can be virtual and redefined in heirs. Semantics of a virtual operator== is correct, and obviously, C++ supports it.
The big lack of C++ is that it doesn't allow us to do things like this :

Code:
class A
{
   virtual bool operator== (const [b]A[/b]& other) const;
};
class B : public A
{
   virtual bool operator== (const [b]B[/b]& other) const;
};

We are obliged to keep the same types for the formal arguments, that leads to use non-safe mechanisms like RTTI to simulate this.


--
Globos
 
Sorry. Had my mind set on assignment operator. [hammer]

Back to your question: Calling A's operator from B's operator would be:
Code:
bool res = A::operator == (other);

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top