Virtual Function with Different Return Type 
Author Message
 Virtual Function with Different Return Type

Can I give a virtual function in a derived class a different return type
that the one in the base class?

ie:

class base{
    virtual base* ptr() { return this; }

Quote:
}

class derived:: public base{
    virtual derived* ptr() { return this; }

Quote:
}

Stroustrup seems to say that this is just fine ( The C++ Programming
Language, 3rd Edition  Section 15.6.2  pg 424). However, VC++ 6.0 says
"error C2555: 'Cond::new_expr' : overriding virtual function differs from
'Expr::new_expr' only by return type or calling convention."


Sat, 28 Dec 2002 03:00:00 GMT  
 Virtual Function with Different Return Type

Quote:
> Can I give a virtual function in a derived class a different return type
> that the one in the base class?

> ie:

> class base{
>     virtual base* ptr() { return this; }
> }

> class derived:: public base{
>     virtual derived* ptr() { return this; }
> }

> Stroustrup seems to say that this is just fine ( The C++ Programming
> Language, 3rd Edition  Section 15.6.2  pg 424). However, VC++ 6.0 says
> "error C2555: 'Cond::new_expr' : overriding virtual function differs from
> 'Expr::new_expr' only by return type or calling convention."

The C++ standard says "yes", but unfortunately VC++ has not caught up with
the standard yet (and neither have many other compilers).

NeilB



Sat, 28 Dec 2002 03:00:00 GMT  
 Virtual Function with Different Return Type


Quote:
> Can I give a virtual function in a derived class a different return type
> that the one in the base class?

> ie:

> class base{
>     virtual base* ptr() { return this; }
> }

> class derived:: public base{
>     virtual derived* ptr() { return this; }
> }

> Stroustrup seems to say that this is just fine ( The C++ Programming
> Language, 3rd Edition  Section 15.6.2  pg 424). However, VC++ 6.0 says
> "error C2555: 'Cond::new_expr' : overriding virtual function differs from
> 'Expr::new_expr' only by return type or calling convention."

I think you're on to something here.  I get the same errors in VC++ 6.3
after duplicating the Stoustup's example on pp.424-5.
It sure seems this ought to work per Stroustrup's discussion.  hmmm....

Compile errors are:
D:\Code\vRetFunc\vRetFunc.cpp(21) : error C2555: 'Cond::new_expr' :
overriding virtual function differs from 'Expr::new_expr' only by return
type or calling convention
        D:\Code\vRetFunc\vRetFunc.cpp(7) : see declaration of 'Expr'
D:\Code\vRetFunc\vRetFunc.cpp(22) : error C2555: 'Cond::clone' : overriding
virtual function differs from 'Expr::clone' only by return type or calling
convention
        D:\Code\vRetFunc\vRetFunc.cpp(7) : see declaration of 'Expr'

Colin



Sat, 28 Dec 2002 03:00:00 GMT  
 Virtual Function with Different Return Type

Quote:
> Can I give a virtual function in a derived class a different return type
> that the one in the base class?

In general no, unless you do this (what you did)...

Quote:
> class base{
>     virtual base* ptr() { return this; }
> }

> class derived:: public base{
>     virtual derived* ptr() { return this; }
> }

This is a standard feature that VC6 doesn't support. It has been
part of C++ for a while now and people have been crying for MSVC++
to support it for the same ammount of time.

Personally, I don't think it's that big of a deal.  But, that's me.

marco



Sat, 28 Dec 2002 03:00:00 GMT  
 Virtual Function with Different Return Type

Quote:

> Can I give a virtual function in a derived class a different return type
> that the one in the base class?

In C++, yes.  In VC++, no.

--
Chris



Sat, 28 Dec 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Derived return types and virtual functions

2. BUG:C2555 On virtual functions with Covariant Return Types

3. overriding virtual function return type or calling convention error

4. Modifying return type when overriding a virtual function in C++ 5

5. Overriding virtual function differ only by return type ..

6. return type of virtual function

7. BUG: Virtual Functions with Covariant Return Types

8. Overloading function prob with different return types

9. Overloading function prob with different return types

10. localtime function returns different values for different winnt operating systems

11. Function pointer pointing to a different function type

12. Bool return type from virtual method

 

 
Powered by phpBB® Forum Software