By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

covariant return types

P: n/a
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!!!
Dec 18 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Dec 18, 11:11 pm, Rahul <sam_...@yahoo.co.inwrote:
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).
Dec 18 '07 #2

P: n/a
On Dec 18, 11:16 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 18, 11:11 pm, Rahul <sam_...@yahoo.co.inwrote:
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).
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...
Dec 18 '07 #3

P: n/a
On Dec 18, 11:32 pm, Rahul <sam_...@yahoo.co.inwrote:
I tried this on gcc... not sure about the version though...- Hide quoted text -
Try running the command g++ -v to see the version.
Dec 18 '07 #4

P: n/a
Rahul wrote:
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.

--
Ian Collins.
Dec 18 '07 #5

P: n/a
Rahul schrieb:
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
Dec 18 '07 #6

P: n/a
Rahul wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 18 '07 #7

P: n/a
On Dec 18, 3:11 pm, Rahul <sam_...@yahoo.co.inwrote:
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

--
Cholo Lennon
Bs.As.
ARG
Dec 18 '07 #8

P: n/a
Andreas Hammerschmidt wrote:
Rahul schrieb:
>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.
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.
Dec 19 '07 #9

P: n/a
On Dec 18, 7:11 pm, Rahul <sam_...@yahoo.co.inwrote:
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
Dec 19 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.