468,514 Members | 1,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

virtual method with different return types?

I have a base (abstract) class with a public method foo delared as:

virtual BaseClass* foo(..)=0;
I wnat to derive two classes A and B from Baseclass so that I return a
pointer for A and B respectively (i.e. A::foo() returns a A*, and
B::foo() returns a B*).

I notice that if I declare the foo in A and B like this:

A* A::foo(...);
B* B::foo(...);

The compiler barfs. I am thinking of doing the ff:

1). leave the signature unchanged (ie. BaseClass* A::foo(), BaseClass*
B::foo()

2). Create the appropriate pointer in the method and then return it as a
BaseClass*.

I have two questions:

1). Is this the correct way to do this?
2). Can I overload AND overide a function (i.e. can A::foo() take
different arguments?)

Apr 15 '06 #1
2 2099
Bit byte wrote:
I have a base (abstract) class with a public method foo delared as:

virtual BaseClass* foo(..)=0;
I wnat to derive two classes A and B from Baseclass so that I return a
pointer for A and B respectively (i.e. A::foo() returns a A*, and
B::foo() returns a B*).

I notice that if I declare the foo in A and B like this:

A* A::foo(...);
B* B::foo(...);

The compiler barfs.
Sounds like you have a rather old compiler that doesn't accept what is
known as "covariant return types".
I am thinking of doing the ff:

1). leave the signature unchanged (ie. BaseClass* A::foo(), BaseClass*
B::foo()

2). Create the appropriate pointer in the method and then return it
as a BaseClass*.
Sounds like a good work-around.
I have two questions:

1). Is this the correct way to do this?
It's a work-around. A more correct way would be to get a better compiler.
2). Can I overload AND overide a function (i.e. can A::foo() take
different arguments?)


Yes. Remember, though, that you cannot _overload_ a function from another
class. You need to bring it into the same scope by means of a "using"
declaration.

V
--
Please remove capital As from my address when replying by mail
Apr 15 '06 #2
Victor Bazarov wrote:
Bit byte wrote:
I have a base (abstract) class with a public method foo delared as:

virtual BaseClass* foo(..)=0;
I wnat to derive two classes A and B from Baseclass so that I return a
pointer for A and B respectively (i.e. A::foo() returns a A*, and
B::foo() returns a B*).

I notice that if I declare the foo in A and B like this:

A* A::foo(...);
B* B::foo(...);

The compiler barfs.

Sounds like you have a rather old compiler that doesn't accept what is
known as "covariant return types".

I am thinking of doing the ff:

1). leave the signature unchanged (ie. BaseClass* A::foo(), BaseClass*
B::foo()

2). Create the appropriate pointer in the method and then return it
as a BaseClass*.

Sounds like a good work-around.

I have two questions:

1). Is this the correct way to do this?

It's a work-around. A more correct way would be to get a better compiler.

2). Can I overload AND overide a function (i.e. can A::foo() take
different arguments?)

Yes. Remember, though, that you cannot _overload_ a function from another
class. You need to bring it into the same scope by means of a "using"
declaration.

V

BTW, same thing is possible to do without using the keyword "using".
This work in g++ and C++ Standard includes the correspondent syntactic
production (although I do not think the meaning of this kind of
declaration is explicitly explained anywhere in the Standard)
for example:
#include <cstdio>
using namespace std;
struct A {
virtual void foo() { printf("A::foo\n"); }
};
struct B : public A {
A::foo;
void foo(int i) { printf("B::foo, i=%d\n"); }
};
int main() {
B b;
b.foo();
b.foo(5);
}
Apr 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by richard.forrest1 | last post: by
11 posts views Thread by santosh | last post: by
8 posts views Thread by Floogle | last post: by
5 posts views Thread by Paul E Collins | last post: by
6 posts views Thread by Alden Pierre | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.