[Faq-20.6] What is a "virtual constructor"?

R

Rolf Magnus

Alex said:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.6

Faq-20.6 contains function userCode(Shape&) :

void userCode(Shape& s)
{
Shape* s2 = s.clone();
Shape* s3 = s.create();
...
// stuff; for instance
// s2->draw();
delete s2;
delete s3;
}

Why don't use userCode2(Shape*) ?
void userCode2(Shape* s)
{
//stuff; for instance
// s->draw();
}

Looks to me just like an example that isn't chosen too well. The idea
seems to be that you work on a copy, not the original object.
 
A

Alex Vinokur

Rolf Magnus said:
Looks to me just like an example that isn't chosen too well. The idea
seems to be that you work on a copy, not the original object.

Why should we work on a copy?
 
B

Bill Seurer

Alex said:
Why should we work on a copy?

The example may not show it but you often want to take something and
manipulate it without changing the original. Thus you need a copy.
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top