473,569 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

override virtual, covariant return type

Hi everyone

I'm having huge difficulties overriding a virtual function with a function
that returns a covariant type. I'd be grateful if someone could show me some
code where this is done:
- A class BaseClass is declared with a function me() that takes no arguments
and returns itself, or a pointer or reference to itself.
- A derived class DerivedClass that override me() that takes no arguments
and returns itself, a pointer or reference to itself ("itself" being the
instance of the derived class)
- A piece of code like this: (Warning: this is PSUEDOCODE)
BaseClass foo=new DerivedClass
foo.me() <-this should return a pointer or reference or instance of type
DerivedClass.

I would be VERY VERY gratefull if someone could help me with this
confounding problem, be it in C++ or C#.

Cheers
dave
Jul 23 '05 #1
12 2049
"David Sobey" <ds****@NOSPAMu grad.unimelb.ed u.au> wrote in message
news:42******** @dnews.tpgi.com .au...
Hi everyone

I'm having huge difficulties overriding a virtual function with a function
that returns a covariant type. I'd be grateful if someone could show me some code where this is done:
- A class BaseClass is declared with a function me() that takes no arguments and returns itself, or a pointer or reference to itself.
- A derived class DerivedClass that override me() that takes no arguments
and returns itself, a pointer or reference to itself ("itself" being the
instance of the derived class)
- A piece of code like this: (Warning: this is PSUEDOCODE)
BaseClass foo=new DerivedClass
This will have to be:
BaseClass *foo = new DerivedClass;
foo.me() <-this should return a pointer or reference or instance of type
foo->me();
DerivedClass.
Since foo is a pointer to a BaseClass, foo->me() will return a BaseClass*.
However, that BaseClass* will point to a DerivedClass object.
I would be VERY VERY gratefull if someone could help me with this
confounding problem, be it in C++ or C#.


You won't get any C# in this place, but here is a C++ example:

class BaseClass
{
public:
virtual BaseClass *me() const { return new BaseClass(*this ); }
// more members
};

class DerivedClass : public BaseClass
{
public:
DerivedClass *me() const { return new DerivedClass (*this); }
// more members
};

int main()
{
BaseClass *foo = new DerivedClass;
BaseClass *p = foo->me(); // p points to a DerivedClass object
}

It is the convention to name such a virtual function 'clone' rather than
'me'.

DW
Jul 23 '05 #2
No way!! if it executes the derived class function, which states explicitly
the return type is DerivedClass*, it will HAVE to return a DerivedClass*!
Man this driving me mad! I so nearly solved this thing. lol. Are you sure
this can't be achieved? Please?!

Cheers
Dave
Jul 23 '05 #3
* David Sobey:
No way!! if it executes the derived class function, which states explicitly
the return type is DerivedClass*, it will HAVE to return a DerivedClass*!
Man this driving me mad! I so nearly solved this thing. lol. Are you sure
this can't be achieved? Please?!


It's unclear what you're confused about, but try to actually read David
White's response.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
I'm not confused, just annoyed about this:

quote: "Since foo is a pointer to a BaseClass, foo->me() will return a
BaseClass*."

I wanted it to return a DerivedClass*.

Oh well.
Cheers
dave
Jul 23 '05 #5
* David Sobey:
I'm not confused, just annoyed about this:

quote: "Since foo is a pointer to a BaseClass, foo->me() will return a
BaseClass*."

I wanted it to return a DerivedClass*.


That is inconsistent with the requirement of covariance.

But it's easy to arrange.

class DerivedClass;

class BaseClass
{
public:
virtual DerivedClass* me() const = 0;
};

However, that's probably not what you want; I suspect you want
something like the visitor pattern (google) to avoid casting.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
"David Sobey" <ds****@NOSPAMu grad.unimelb.ed u.au> wrote in message
news:42******@d news.tpgi.com.a u...
No way!! if it executes the derived class function, which states explicitly the return type is DerivedClass*, it will HAVE to return a DerivedClass*!
Man this driving me mad! I so nearly solved this thing. lol. Are you sure
this can't be achieved? Please?!


The function does return a DerivedClass*, but you receive it as a BaseClass*
because that's the return type of the function you called. What you seem to
want doesn't really make sense. Consider the following:

// ignore all the memory leaks

void f(BaseClass *p)
{
BaseClass *q = p->me();
}

int main()
{
f(new BaseClass);
f(new DerivedClass);
}

For the first call of function f, BaseClass::me will be called inside the
function and return a BaseClass*. For the second call DerivedClass::m e will
be called and return a DerivedClass*. You can see that the function f only
knows that 'p' points to a BaseClass or a class derived from it, and the
actual class of the object that 'p' points to can vary from one call to
another. It obviously can't assume that it's going to receive a
DerivedClass* from the call to me() because that will only happen when 'p'
really points to a DerivedClass, and that's not necessarily the case. If it
knew that then it could change its parameter type:

void f(DerivedClass *p)
{
DerivedClass *q = p->me();
}

Now it knows that 'q' points to a DerivedClass object, or an object of a
class derived from DerivedClass, but you can't call it with any old
BaseClass* now.

If you always knew which class's virtual function would be called at a given
statement in your code, there would be no need for the function to be
virtual. Virtual functions are only useful when you don't know which one
will be called until run-time.

DW
Jul 23 '05 #7

"David Sobey" <ds****@NOSPAMu grad.unimelb.ed u.au> wrote in message
news:42******** @dnews.tpgi.com .au...
Hi everyone

I'm having huge difficulties overriding a virtual function with a function
that returns a covariant type. I'd be grateful if someone could show me
some code where this is done:
- A class BaseClass is declared with a function me() that takes no
arguments and returns itself, or a pointer or reference to itself.
- A derived class DerivedClass that override me() that takes no arguments
and returns itself, a pointer or reference to itself ("itself" being the
instance of the derived class)
- A piece of code like this: (Warning: this is PSUEDOCODE)
BaseClass foo=new DerivedClass
foo.me() <-this should return a pointer or reference or instance of type
DerivedClass.

I would be VERY VERY gratefull if someone could help me with this
confounding problem, be it in C++ or C#.

Cheers
dave


You state the function returns "itself, or a pointer or reference to
itself". Well, which is it? Pick one.

Given:
BaseClass* p1 = new DerivedClass;

This gives you a pointer to the same object:
BaseClass* p2 = p1; // points to same object as p1 points to

This gives you a reference to that object:
BaseClass& r2 = *p1; // refers to the object p1 points to

Or perhaps you meant "a copy of itself" when you said "itself"... ?

This gives you a copy of p1 (assuming a copy constructor is available):
BaseClass* p2 = new DerivedClass(*p 1); // creates new object using given
object

If your function returns a DerivedClass*, remember that you can always
assign that to a BaseClass*. So:
BaseClass* p2 = p1->foo(); // foo returns a DerivedClass*
will give you a DerivedClass*, but you've simply stored it in a BaseClass*,
which is acceptable, and a common technique used for polymorphic behavior,
especially when using containers of pointers. You could not do the same
with an actual object, because "slicing" would occur. But with pointers and
references, it's fine.

Also, as suggested, Google for the term "clone". (Try "C++ clone function".)

-Howard



Jul 23 '05 #8
PV
Try this: (simple and stupid, but it does what you want)
---------------------------------------------

#include <iostream>

using namespace std;

class Derived;

class Base
{
public:
virtual ~Base() {}

virtual char* name() const
{
return "Base";
}

virtual Base* me()
{
return this;
}

virtual Derived* getDerived()
{
return NULL;
}

};

class Derived : public Base
{
public:
virtual char* name() const
{
return "Derived";
}

Base* me()
{
return this;
}

Derived* getDerived()
{
return this;
}
};

int main()
{
Base* b = new Derived();
Derived* d = b->getDerived() ;

cout << d->name() << endl;

delete b;
}

---------------------------------------------------

"David Sobey" <ds****@NOSPAMu grad.unimelb.ed u.au> wrote in message
news:42******** @dnews.tpgi.com .au...
I'm not confused, just annoyed about this:

quote: "Since foo is a pointer to a BaseClass, foo->me() will return a
BaseClass*."

I wanted it to return a DerivedClass*.

Oh well.
Cheers
dave

Jul 23 '05 #9

PV wrote:
Try this: (simple and stupid, but it does what you want)
---------------------------------------------

#include <iostream>

using namespace std;

class Derived;

class Base
{
public:
virtual ~Base() {}

virtual char* name() const
{
return "Base";
}

virtual Base* me()
{
return this;
}

virtual Derived* getDerived()
{
return NULL;
}

};

class Derived : public Base
{
public:
virtual char* name() const
{
return "Derived";
}

Base* me()
{
return this;
}

Derived* getDerived()
{
return this;
}
};

int main()
{
Base* b = new Derived();
Derived* d = b->getDerived() ;

cout << d->name() << endl;

delete b;
}

---------------------------------------------------

It may do what the OP wants, but its violating one of the most important
aspects of a good OO design, namely that base classes should not know of
their derived types.

If the design means that a base class has to know about their derived
classes, then the design is wrong. If the OP wants we could explorer
ways which would remove the need for usage of this base <-> derived
coupling this anti-pattern.

Andrew

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2142
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
3
1685
by: Matt | last post by:
Hello, I'm trying to implement the design below. I'd prefer to use commented operator+() in class Number instead of the associated, uncommented operator+(), but as I understand Covariant Return Types, one is not allowed to return non-references (eg, references or pointers) in virtual-functions (which seems to make sense given the seemingly...
6
2051
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding or hiding a base function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A class BaseClass is declared with a function me() that takes no arguments and returns itself, or a pointer or reference to itself. - A...
15
3771
by: Cliff_Harker | last post by:
Why can't I do this in C# public class A { public A virtual whatever( A a ) { } } public class B : A
5
2380
by: Squeamizh | last post by:
The subject line probably isn't the best, but I don't know how else to describe my question. When I try to compile the code below, my compiler reports an error (error text is below code sample). class GrandParent { virtual void doSomething() = 0; };
8
2170
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
3
4541
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from...
4
1617
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function
4
5971
by: chambersdon | last post by:
I need to create an abstract class but have the method in the derived class differ by the return type. This is my code: //Abstract class public abstract class DataItem { public abstract DbDataReader getRecord(); } //Derived Class public class DistributionCode:DataItem
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.