Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
Hope, all are doing well.
I have a confusion in my mind.
I have tried to find out the answer, but couldn't understood.
The question is, What is a partially constructed object in C++?

What I have tried:

Tried to find out what is partially constructed object.
Posted
Updated 31-Jan-22 5:06am

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your subject as the search term gave nearly 5 million hits: : What is a partially constructed object in C - Google Search[^]
And every one I looked at discussed this in depth ...

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Comments
Member 14036158 30-Jan-22 13:57pm    
Hi OriginalGnff,
Thanks for your suggestion.
I have posted the question here with hope because I searched and couldn't found the clear explanation about partial construction using static and dynamic both. allocation.
So that I posted.
When an object in a derived class is constructed, constructors are invoked from the root base class down to the leaf derived class. Until the leaf constructor finishes executing, the object is only partially constructed.

Let's say that some constructor adds the object to a container or makes it accessible in some other way. If other code then invokes a virtual function on the object, the resulting behavior is unpredictable if a derived class that has not yet been constructed overrides that function, because a base class version of the function will be invoked instead.

When the object is deleted, destructors are invoked from the leaf class up to the root base class. The same problem can arise here, only worse, because the virtual function might be pure virtual (i.e., unimplemented) when the destructor chain reaches some base class.

The safest practice is not to make an object available (by exporting a pointer or reference to it) until it has been fully constructed (edit: and to make it unavailable while it is being deleted). If you have a reason for violating this guideline, you need to be sure that doing so will not lead to problems.

EDIT: Another way to run into trouble is if a constructor invokes a virtual function in its own class hierachy. The virtual function that gets invoked is the one in the class whose constructor is currently running. This can be a problem because the object's data is still subject to change. It also means that a base class can't ask a derived class to do something by invoking a virtual function.

Also, when member initializers (those above the body of a constructor) are being executed, the object is still considered to be in the immediate base class, not the class being initialized.
 
Share this answer
 
v3
Comments
Member 14036158 31-Jan-22 11:22am    
Thanks Greg.
I value the help you've given me.
Greg Utas 31-Jan-22 11:32am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900