473,734 Members | 2,788 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
12 2071
"PV" <pv******@shaw. ca> wrote in message
news:nuY2e.8604 62$Xk.481117@pd 7tw3no...
Try this: (simple and stupid, but it does what you want)


[snip]

I used to do this before there was a dynamic_cast, or when my compiler
didn't have dynamic_cast, but I didn't like it for the reason that Andrew M.
gave. Now I can't see why this would ever be preferred to dynamic_cast.

DW
Jul 23 '05 #11
I wrote in message news:Yg******** **********@nasa l.pacific.net.a u...
"David Sobey" <ds****@NOSPAMu grad.unimelb.ed u.au> wrote in message
news:42******** @dnews.tpgi.com .au...
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.
class BaseClass
{
public:
virtual BaseClass *me() const { return new BaseClass(*this ); }
// more members
};


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


Sorry, I misunderstood that a clone function was requested, but it really
was a 'me' function. It doesn't affect the rest of my answers though.

DW
Jul 23 '05 #12
This would be preffered because when there are lots and lots of direct
children of base class, ie lots of DerivedClass's, and a function that
excepts two BaseClass pointers as it's arguments, dynamic_casting would be a
nightmare, as there would be an exponential number of combinations of
casting.

cheers
dave
Jul 23 '05 #13

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

Similar topics

7
2148
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
1693
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 polymorphic nature). So...I use the hack below and make my Object-return function be
6
2058
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 derived class DerivedClass that override me() that takes no arguments and returns itself, a pointer or...
15
3784
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
2412
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
2175
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
4550
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 Triangle and Prism, or Square and Prism respectively
4
1627
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
5980
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
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9449
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.