covariant return types

R

Rahul

Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!
 
A

Abhishek Padmanabh

Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{

};

What are these classes? BB/AA?
class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}

};

Returning pointer to a local object!!
class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}

};

Returning pointer to a local object!!
'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

What compiler is it? I think it should compile just fine if you
correct the local object pointer return thing, and remove the class BB
and AA (they are useless in the rest of your code).
 
R

Rahul

What are these classes? BB/AA?



Returning pointer to a local object!!



Returning pointer to a local object!!





What compiler is it? I think it should compile just fine if you
correct the local object pointer return thing, and remove the class BB
and AA (they are useless in the rest of your code).

Ignore the dummy class BB and the logic of the function returning
address of local object.
Yes i do get a warning from the compiler, however the error is with
the return type of functions.
I tried this on gcc... not sure about the version though...
 
I

Ian Collins

Rahul said:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);

Change to return new A;
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);

Change to return new B;
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?
There isn't anything wrong with the return types, just the returns of
local objects.
 
A

Andreas Hammerschmidt

Rahul said:
Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!

Hi!

The return type is part of the internal method name. When overloading a
method you need exactly the same internal name and - additionally - it
is not possible to make two identical methods which differ only in the
return type.

Maybe you could templates for this.

Andreas
 
V

Victor Bazarov

Rahul said:
I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{
};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}
};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}
};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Unless you actually forgot to derive 'B' from 'A' in your real
code, the types of functions 'A::sample' and 'B::sample' are OK
(they are covariant). You may be missing the fact that you're
using an outdated compiler that doesn't understand covariant
return types.

V
 
C

Cholo Lennon

Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

class BB : public AA
{

};

class A
{
public: virtual A* sample()
{
A obj;
return(&obj);
}

};

class B : public A
{
public: virtual B* sample()
{
B obj;
return(&obj);
}

};

'B::sample' : overriding virtual function differs from 'A::sample'
only by return type or calling convention

Am i missing something?

Thanks in advance!!!

You are using VC++ 6, which doesn't support covariant return types.
Please update your compiler, it's too old...


Regards
 
R

red floyd

Andreas said:
Hi!

The return type is part of the internal method name. When overloading a
method you need exactly the same internal name and - additionally - it
is not possible to make two identical methods which differ only in the
return type.

Except in this special case, which is a covariant return. That is, the
override returns a pointer (or reference) to a child of what the base
class function returns.

It's explicitly allowed by the standard.
 
W

werasm

Hi Everyone,

I was trying to implement covariant return types and i get a
compilation error,

The code is supposed to compile. Try compilation under

http://www.comeaucomputing.com/tryitout/

I find it to be very close to the standard and use it as
a benchmark (you could also go to
http://www.dinkumware.com/exam/default.aspx for the same
reason).

If I was you I would at least change the local
members to static members to prevent people from missing
the point of your question. BB/AA also does not seem to
contribute to your question.

Regards,

Werner
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top