Virtual Constructors

C

Carl R. Davies

I was reading this link http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198
heading "14.10 Virtual Constructors"

I am struggling to understand the issue the author is trying to solve.
This paragraph is confusing me:

"As we have seen (section 14.2) C++ supports virtual destructors. Like
many other object oriented languages (e.g., Java), however, the notion
of a virtual constructor is not supported. The absence of a virtual
constructor turns into a problem when only a base class reference or
pointer is available, and a copy of a derived class object is
required."

What's wrong with down casting the base class pointer and using
assignment?

I have a feeling the answer is simple and I'm just being dense :)
 
D

dave_mikesell

I was reading this linkhttp://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198
heading "14.10 Virtual Constructors"

I am struggling to understand the issue the author is trying to solve.
This paragraph is confusing me:

"As we have seen (section 14.2) C++ supports virtual destructors. Like
many other object oriented languages (e.g., Java), however, the notion
of a virtual constructor is not supported. The absence of a virtual
constructor turns into a problem when only a base class reference or
pointer is available, and a copy of a derived class object is
required."

What's wrong with down casting the base class pointer and using
assignment?

Bad design. If you design your class hierarchy correctly, client code
should be unaware of derived classes and work only through base class
pointers.
 
V

Victor Bazarov

Carl said:
I was reading this link
http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198
heading "14.10 Virtual Constructors"

I am struggling to understand the issue the author is trying to solve.
This paragraph is confusing me:

"As we have seen (section 14.2) C++ supports virtual destructors. Like
many other object oriented languages (e.g., Java), however, the notion
of a virtual constructor is not supported. The absence of a virtual
constructor turns into a problem when only a base class reference or
pointer is available, and a copy of a derived class object is
required."

What's wrong with down casting the base class pointer and using
assignment?

Assignment and construction are two different things. How would
you "use assignment" when you need to pass an object to a function?
You'd be creating an unnecessary local object, which is unnecessary
if there are virtual constructors...
I have a feeling the answer is simple and I'm just being dense :)

<shrug>

What is often referred to as "a virtual constructor" is a "clone"
method. If you never had to use one, don't worry, you're bound to,
eventually. When you need it, you'll find why it is needed and how
to implement it.

V
 
R

red floyd

Victor said:
What is often referred to as "a virtual constructor" is a "clone"
method. If you never had to use one, don't worry, you're bound to,
eventually. When you need it, you'll find why it is needed and how
to implement it.

That's a "virtual copy constructor". The other type of "virtual
constructor" is a factory pattern.
 
C

Carl R. Davies

Bad design. If you design your class hierarchy correctly, client code
should be unaware of derived classes and work only through base class
pointers.

Ok, is it because the client code might not know what the derived
class is at time of writing so it's unable to down cast?

1. Framework code declares classes Base and Derived " class Derived :
public Base "
2. So in the client code:

Derived *ptr = new Derived();

Then calls a function that only knows Base:

void func( Base *inPtr )
{
// Can't down cast because doesn't know existence of Derived
// So how does Clone() help here?
}

Apologies for being persistent, I would like to know so I can
recognise the need for "Prototype Pattern" in the future.

Thanks.
 
V

Victor Bazarov

Carl said:
Ok, is it because the client code might not know what the derived
class is at time of writing so it's unable to down cast?

1. Framework code declares classes Base and Derived " class Derived :
public Base "
2. So in the client code:

Derived *ptr = new Derived();

Then calls a function that only knows Base:

void func( Base *inPtr )
{
// Can't down cast because doesn't know existence of Derived
// So how does Clone() help here?
}

Apologies for being persistent, I would like to know so I can
recognise the need for "Prototype Pattern" in the future.

class clonable {
virtual clonable* clone() const = 0;
};

class Base : public clonable {
...
Base* clone() const { return new Base(*this); }
};

class Derived : public Base {
...
Derived* clone() const { return new Derived(*this); }
};

void func(Base *inPtr)
{
Base *newOne = inPtr->clone();
}

V
 
C

Carl R. Davies

class clonable {
virtual clonable* clone() const = 0;

};

class Base : public clonable {
...
Base* clone() const { return new Base(*this); }

};

class Derived : public Base {
...
Derived* clone() const { return new Derived(*this); }

};

void func(Base *inPtr)
{
Base *newOne = inPtr->clone();

}

Thanks Victor, I now understand what it does and importantly "Why?".
Cheers everyone.
 

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,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top