result covariant

T

Tony Johansson

Hello!!

When you have result covariance is it then necessary that you have
inheritance.

I mean can you have result covariance without having inheritance.

//Tony
 
M

mlimber

Tony said:
Hello!!

When you have result covariance is it then necessary that you have
inheritance.

I mean can you have result covariance without having inheritance.

//Tony

Inheritance is necessary. Even void* won't work. Try this:

struct A
{
virtual A* Create() const { return new A; }
virtual int* GetVal() const { return new int(); }
};

struct B : public A
{
virtual B* Create() const { return new B; } // Ok
virtual char* GetVal() const { return new char(); } //Error!
};

Cheers! --M
 
V

Victor Bazarov

Tony said:
When you have result covariance is it then necessary that you have
inheritance.

I mean can you have result covariance without having inheritance.

"Covariance" by definition requires inheritance. Is it necessary for
a bicycle to have wheels?
 
M

mlimber

Victor said:
"Covariance" by definition requires inheritance. Is it necessary for
a bicycle to have wheels?

But, Victor, that's "has-a" inheritance! Surely, you meant something
like "Is it necessary for a bicycle to be a mountain bike?" ;-)

Cheers! --M
 
V

Victor Bazarov

mlimber said:
But, Victor, that's "has-a" inheritance! Surely, you meant something
like "Is it necessary for a bicycle to be a mountain bike?" ;-)

I think you're confusing what my analogy is for. It is necessary by
definition for a bicycle to have wheels to be a bicycle. It is necessary
by definition for covariance to involve inheritance. I wasn't implying
that a bicycle was covariant with wheels. Yes, a mountain bike would
probably be covariant with a bike if it inherited from it publicly.

V
 

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,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top