472,984 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

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 2316
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
5
by: Paul E Collins | last post by:
I have a class hierarchy representing data importers, each of which reads lines from a particular type of comma-separated data file and creates corresponding entries in a database. There is an...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
2
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.