473,698 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constructors as virtual

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
virtual void move() = 0;
...
virtual Shape* clone() const = 0; // Uses the copy constructor
virtual Shape* create() const = 0; // Uses the default
constructor
};

class Circle : public Shape {
public:
Circle* clone() const; // Covariant Return Types; see below
Circle* create() const; // Covariant Return Types; see below
...
};

Circle* Circle::clone() const { return new Circle(*this); }
Circle* Circle::create( ) const { return new Circle(); }

Now, new Circle() would create a Circle object, and the constructor of
bsae class Shape would be called first before Circle right? So how
does it offer to be a workaround for the constructors being virtual?

Thanks in advance!!!
Jan 2 '08 #1
4 1624
Rahul wrote:
Hi Everyone,

I understand that the constructors can't be virtual
Good. Do you also understand why?
and parashift has the following example, to have an workaround for the
constructors to be virtual,
IMHO, that is not really a good description for what that example does.
class Shape {
public:
virtual ~Shape() { } // A virtual destructor
virtual void draw() = 0; // A pure virtual function
virtual void move() = 0;
...
virtual Shape* clone() const = 0; // Uses the copy constructor
virtual Shape* create() const = 0; // Uses the default
constructor
};

class Circle : public Shape {
public:
Circle* clone() const; // Covariant Return Types; see below
Circle* create() const; // Covariant Return Types; see below
...
};

Circle* Circle::clone() const { return new Circle(*this); }
Circle* Circle::create( ) const { return new Circle(); }

Now, new Circle() would create a Circle object, and the constructor of
bsae class Shape would be called first before Circle right? So how
does it offer to be a workaround for the constructors being virtual?
For that, you would first have to explain what you would expect from
a "virtual constructor", since such a concept wouldn't make any sense in
C++.
Jan 2 '08 #2
On 2 ÑÎ×, 23:22, Rahul <sam_...@yahoo. co.inwrote:
Now, new Circle() would create a Circle object, and the constructor of
bsae class Shape would be called first before Circle right? So how
does it offer to be a workaround for the constructors being virtual?
Don't virtual destructors make you curious as well? ;-)
Jan 2 '08 #3
Rahul wrote:
:: 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
:: virtual void move() = 0;
:: ...
:: virtual Shape* clone() const = 0; // Uses the copy constructor
:: virtual Shape* create() const = 0; // Uses the default
:: constructor
:: };
::
:: class Circle : public Shape {
:: public:
:: Circle* clone() const; // Covariant Return Types; see below
:: Circle* create() const; // Covariant Return Types; see below
:: ...
:: };
::
:: Circle* Circle::clone() const { return new Circle(*this); }
:: Circle* Circle::create( ) const { return new Circle(); }
::
:: Now, new Circle() would create a Circle object, and the
:: constructor of bsae class Shape would be called first before
:: Circle right? So how does it offer to be a workaround for the
:: constructors being virtual?

Because if you have a pointer p to an object, p->clone() will get you
another object of the same type, even when you don't know the exact
type.

Bo Persson
Jan 2 '08 #4
On Jan 2, 7:32 pm, Rolf Magnus <ramag...@t-online.dewrote:
Rahul wrote:
I understand that the constructors can't be virtual
Good. Do you also understand why?
and parashift has the following example, to have an workaround for the
constructors to be virtual,
IMHO, that is not really a good description for what that example does.
Yes and no. The characteristic of a virtual function is that
the actual function called depends on the dynamic type of the
object it is called on. In the case of a constructor, of
course, the object doesn't exist until after the contructor is
called, but a clone() function is the next best thing: it
creates an object according to the dynamic type of an existing
object. (Of course, you have to think "constructo r" in a more
abstract way---maybe as anything which "constructs ".)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 2 '08 #5

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

Similar topics

7
13626
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember correctly) as: ------------------------------------------- class Base { public: Base(); }; Base::Base() { printf("Base"); } class Derived : Base { public: Derived(); override; }; Derived::Derived() { /*inherited;*/ printf("Derived"); }
42
5780
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
4
1444
by: pixelbeast | last post by:
hi, In the following code, I would like not to have to declare the constructors in bar ( either the default or the (int a) constructor ). When I remove them, bar has no idea of construction with an int, so I get compile errors. I currently have to declare them for every class inheriting foo, and it would be much nicer just with the init. Is there a nice way of doing this? I am currently using a macro, like CONSTRUCTOR(bar) which feels a...
5
3775
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in C++ ? waitng for the answers.
22
5197
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
68
3510
by: Peter Morris [Droopy eyes software] | last post by:
I have an object persistence framework I have written, this framework expects every object to descend ultimately from PersistentObject and to have a constructor (ObjectSpace objectSpace) so that objects may be recreated. PersistentObject's constructor will do something like this this.ObjectSpace = objectSpace; objectSpace.RegisterObjectCreation(this); The object space will record a reference to this new object + do something
7
3734
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
8
2329
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
0
8680
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
9030
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
8899
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,...
0
8871
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
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
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
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.