Click to See Complete Forum and Search --> : C++ programming


Ilia Faingold
May 16th, 1999, 12:56 PM
Hi all,

I have a question about C++ standard. According to [Stroustrup, C++ Programming Language, 3d ed., p.425], there is one case when the return type of overriding function may differ from the virtual function it overrides. Namely, if the original return type was B*, then the return type of the overriding function may be D*, if B is a public base of D.

In my programm I have three classes,

class Base
{
...
virtual Base* f() { return 0; }
...
}

class Derived1: public Base
{
...
}

class Derived2: public Derived1
{
...
Derived2* f() {...}
...
}

According to the standard (as far as I understand it), this should work. However, the Microsoft Visual C++ 6.0 compiler throws me error C2555. Where the truth?

Thank you for help,
Ilia.

Playman Cheng
May 17th, 1999, 06:37 AM
Baby, you override the virtual function,
What you do is not deriving the fuction,
but is overriding.Check the parameters,
return value please.

Dave Lorde
May 17th, 1999, 07:03 AM
You are correct in thinking this should work. This feature is known as 'covariant return types', and has been in the standard for quite some time. It is extremely useful, especially when writing clone functions for polymorphic objects, but Microsoft have so far been unable or unwilling to support it in VC++.

Unfortunately, it is just another example of VC++6.0's lack of standard compliance :-(

You will have to work around it as best you can.

Dave