Topic: Virtual operator function mystery


Author: admin@rzaix13.uni-hamburg.de (Bernd Eggink)
Date: 21 Oct 1994 11:45:17 GMT
Raw View
Is there a special rule against declaring an operator function
virtual in a derived class that isn't virtual in the base class?

My test program:

------------------------------------------------

#include <iostream.h>

class V: public ostream
{
  public:
 V(): ostream(cout.rdbuf()) { }

 virtual V &operator<<(int x)
 { (ostream &)*this << x; return *this; }

 virtual V &operator<<(char *x)
 { (ostream &)*this << x; return *this; }
};

class D: public V
{
  public:
 D &operator<<(int x)
 { ostream::operator<<(x); return *this; }

};  // <== (1)


main()
{
 D d, &e = d;

 e << 1;
 e << "   " << 12.34;  // <== (2)

 return 0;
}

------------------------------------------------

Three compilers (BC4.02, xlC, Sun CC) give the error message

 "D::operator<<(int) hides virtual function V::operator<<(char *)"

at the line marked (1), which sounds like nonsense to me.

g++ 2.6.0 doesn't, but complains about "invalid operands to binary << "
at (2).

Can anybody comment this? I found nothing about this kind of derivation
neither in the ARM nor in the latest draft.

Thanks!

--
+----------------------------------+
|          Bernd Eggink            |
|    Rechenzentrum Uni Hamburg     |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+




Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 24 Oct 1994 22:38:35 -0500
Raw View
admin@rzaix13.uni-hamburg.de (Bernd Eggink) writes:

>Is there a special rule against declaring an operator function
>virtual in a derived class that isn't virtual in the base class?

>My test program:

>------------------------------------------------

>#include <iostream.h>

>class V: public ostream
>{
>  public:
> V(): ostream(cout.rdbuf()) { }

> virtual V &operator<<(int x)
> { (ostream &)*this << x; return *this; }

> virtual V &operator<<(char *x)
> { (ostream &)*this << x; return *this; }
>};

>class D: public V
>{
>  public:
> D &operator<<(int x)
> { ostream::operator<<(x); return *this; }

>};  // <== (1)


>main()
>{
> D d, &e = d;

> e << 1;
> e << "   " << 12.34;  // <== (2)

> return 0;
>}

>------------------------------------------------

>Three compilers (BC4.02, xlC, Sun CC) give the error message

> "D::operator<<(int) hides virtual function V::operator<<(char *)"

>at the line marked (1), which sounds like nonsense to me.

>g++ 2.6.0 doesn't, but complains about "invalid operands to binary << "
>at (2).

>Can anybody comment this? I found nothing about this kind of derivation
>neither in the ARM nor in the latest draft.

This is an absolutely correct warning message, that most C++ compilers
issue. The fact that the operator << that you defined is virtual has
absolutely nothing to do with the error. What matters is that a base
class has an operator << member function with different arguments.

The c++ language has a rule that a member function in a derived
class hides ALL base class functions with the same name. For an
overloaded operator, this means all overloadings of the same
operator. (There is some debate as to whether this should apply
to operators with both unary and binary forms.).

In your case, the ostream bas class has several operator<< functions.
You may need to override all of them in your dervied class.