virtual functions

P

puzzlecracker

is return type considered in the context of virtual functions?

ex.

class Base{

public:
virtual int foo(){}
};

class Derived:public Base{

public:
virtual double foo();

};

On par, are access modifier considered, matter, how? in above context:
let's foo was declared public in Base and in protected in Derived...
and visa versa........

I am trying to compare the polymorphism resolution in C++ to Java,
where in latter there is concept of more/less specific, etc., I always
*assumed* that return value doesn' play any role whatsoever.

Please be as detailed as you have time and desire.

Thanks
 
P

Phlip

puzzlecracker said:
is return type considered in the context of virtual functions?

No, with a narrow exception.
virtual int foo(){}
virtual double foo();

The "signature" is everything from the 'foo' going right (with another
exception of default argument values, which you shouldn't use).

The compiler should reject the above because the signature is not enough to
distinguish.

The exception is "covariant return types". virtual methods may generally
return similar pointers or references related by inheritance.
On par, are access modifier considered, matter, how? in above context:
let's foo was declared public in Base and in protected in Derived...
and visa versa........

No, because that would impose a runtime penalty. Recall you can call a
derived foo from a base class pointer, so if the derived type were private,
the program might only be able to detect this at runtime. Privacy is only a
frail compile-time check, and plenty of design patterns depend on public
interface methods calling private implementations.
I am trying to compare the polymorphism resolution in C++ to Java,
where in latter there is concept of more/less specific, etc., I always
*assumed* that return value doesn' play any role whatsoever.

Java subtracts much from C++ while adding very little. Research Python,
Ruby, or Smalltalk to learn how different inheritance systems can be.
 
I

Ian Collins

Phlip said:
Java subtracts much from C++ while adding very little. Research Python,
Ruby, or Smalltalk to learn how different inheritance systems can be.
Or JavaScript :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top