Dering classes, virtual functions, not sure return type

  • Thread starter Chris Mantoulidis
  • Start date
C

Chris Mantoulidis

I have an abstract class BaseDate and have declared its functions as
pure virtual.

Then I have another class MathDate whose parent is BaseDate.

In base date I have this functions (among others):

virtual BaseDate * set_day(int) = 0;

I've got that function for month and year handling as well. I wanted
to have BaseDate & as a return type but I wasn't sure if I could
achieve polymorphism with a reference (can I?). Still even with a
reference my problem would be the same.

Since that function is virtual I have to use that declaration in the
deriving class, so I'm stuck with

BaseDate * set_day(int);

in my MathDate class. However I want the MathDate class to return
MathDate & (or MathDate * anyway). I would have used overloading but
since this class is for my practise with abstract classes I was
wondering if there was a way to solve my problem.

TIA,
cmad
 
J

Jacques Labuschagne

Chris said:
I have an abstract class BaseDate and have declared its functions as
pure virtual.

Then I have another class MathDate whose parent is BaseDate.

In base date I have this functions (among others):

virtual BaseDate * set_day(int) = 0;

I've got that function for month and year handling as well. I wanted
to have BaseDate & as a return type but I wasn't sure if I could
achieve polymorphism with a reference (can I?). Still even with a
reference my problem would be the same.

Yes, you can.
Since that function is virtual I have to use that declaration in the
deriving class, so I'm stuck with

BaseDate * set_day(int);

in my MathDate class. However I want the MathDate class to return
MathDate & (or MathDate * anyway). I would have used overloading but
since this class is for my practise with abstract classes I was
wondering if there was a way to solve my problem.

This wouldn't be overloading. The the types are part of the same
hierarchy and they share virtual functions, that's called having
covariant return types.

Go ahead and write the derived member as
MathDate* set_day(int);
It'll do what you want as long as your compiler is standards compliant.

struct A{
virtual ~A(){}
virtual A& foo() = 0;
};
struct B: A{
virtual B& foo(){ return *this; }
};


Jacques
 
D

David White

Chris Mantoulidis said:
I have an abstract class BaseDate and have declared its functions as
pure virtual.

Then I have another class MathDate whose parent is BaseDate.

In base date I have this functions (among others):

virtual BaseDate * set_day(int) = 0;

I've got that function for month and year handling as well. I wanted
to have BaseDate & as a return type but I wasn't sure if I could
achieve polymorphism with a reference (can I?).

Yes. A reference is polymorphic wherever the corresponding pointer is.
Still even with a
reference my problem would be the same.

Since that function is virtual I have to use that declaration in the
deriving class, so I'm stuck with

BaseDate * set_day(int);

No, you can return MathDate* (or MathDate&) if you want. I believe that this
was one of the later additions to the language, so some popular compilers
might not accept it.
in my MathDate class. However I want the MathDate class to return
MathDate & (or MathDate * anyway). I would have used overloading but
since this class is for my practise with abstract classes I was
wondering if there was a way to solve my problem.

Yes. See above.

DW
 
M

Martijn Lievaart

I have an abstract class BaseDate and have declared its functions as
pure virtual.

Then I have another class MathDate whose parent is BaseDate.

In base date I have this functions (among others):

virtual BaseDate * set_day(int) = 0;

Since that function is virtual I have to use that declaration in the
deriving class, so I'm stuck with

BaseDate * set_day(int);

in my MathDate class. However I want the MathDate class to return
MathDate & (or MathDate * anyway). I would have used overloading but
since this class is for my practise with abstract classes I was
wondering if there was a way to solve my problem.

It's called covariant return types, and it is OK. Just return a MathDate*
from a MathDate, a FunnyDate* from a FunnyDate, as long as they derive
from BaseDate, it should work.

Note that some older compilers don't implement this feature yet, if so,
upgrade your compiler. There will be many other things that will not work
on such a compiler.

HTH,
M4
 
C

Chris Mantoulidis

Martijn Lievaart said:
It's called covariant return types, and it is OK. Just return a MathDate*
from a MathDate, a FunnyDate* from a FunnyDate, as long as they derive
from BaseDate, it should work.

Note that some older compilers don't implement this feature yet, if so,
upgrade your compiler. There will be many other things that will not work
on such a compiler.

HTH,
M4

Thanks all! Let's just hope my GCC 3.3.2 will work with that.

I'm not home right now, so I'll check it out as soon as I get back :)
 

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

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top