virtual constructor

M

Method Man

David White said:

I'm having trouble understanding this line from that FAQ:

"This function will work correctly regardless of whether the Shape is a
Circle, Square, or some other kind-of Shape that doesn't even exist yet."

What do they mean by "some other kind-of Shape that doesn't even exist
yet."? Are they talking about a forward declared class? And what does
clone() and create() return if the subtype doesn't exist?
 
A

Alf P. Steinbach

* Method Man:
I'm having trouble understanding this line from that FAQ:

"This function will work correctly regardless of whether the Shape is a
Circle, Square, or some other kind-of Shape that doesn't even exist yet."

The above means that the code calling the virtual constructor does not
need to know the particular kind of Shape. A compiled version of such
code, X, can be linked to other code so that it will be creating a kind
of Shape that didn't exist when X was compiled. The new kind of Shape
is introduced after compilation, but before linking, of X.

By the way, I should mention that the term "virtual constructor" has
another common meaning, namely that of a constructor that can be fully
or partially overridden in a derived class. I think this is the more
natural meaning. However, the first meaning (20.6) was introduced first
in the FAQ, and so the second needed a new term, "Dynamic Binding During
Initialization", or DBDI, which Marshall invented when we introduced a
discussion of techniques for the second meaning in the FAQ.

You find that discussion at <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4>.
 
K

Karl Heinz Buchegger

Method said:
I'm having trouble understanding this line from that FAQ:

"This function will work correctly regardless of whether the Shape is a
Circle, Square, or some other kind-of Shape that doesn't even exist yet."

What do they mean by "some other kind-of Shape that doesn't even exist
yet."? Are they talking about a forward declared class?

Alf has already answered that part
And what does
clone() and create() return if the subtype doesn't exist?

That situation is not possible.
If you think of it, clone() or create() are always applied on an already
existing object. So you *must* already have an object of that type in
order to call clone() or create() on it.
You can think of it this way: The clone() call in

Shape* TheShape = Get_A_Shape_Object_From_Somwehere();
Shape* AnotherShape = TheShape->clone();

can be translated in ordinary english:
Hey object where TheShape points to! I have no idea what you really
are and I don't care. But please create a new object which is a copy
of yourself.
 
M

Method Man

Alf P. Steinbach said:
* Method Man: yet."

The above means that the code calling the virtual constructor does not
need to know the particular kind of Shape. A compiled version of such
code, X, can be linked to other code so that it will be creating a kind
of Shape that didn't exist when X was compiled. The new kind of Shape
is introduced after compilation, but before linking, of X.

Thanks, the wording had me confused.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top