constructors as virtual

R

Rahul

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!!!
 
R

Rolf Magnus

Rahul said:
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++.
 
P

Pavel Shved

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? ;-)
 
B

Bo Persson

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
 
J

James Kanze

Rahul wrote:
Good. Do you also understand why?
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 "constructor" in a more
abstract way---maybe as anything which "constructs".)
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top