473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Object construction...


At which point is an object considered to be completely constructed?

X::X()
{
// constructor stuff

throw "Exception"; // premature exit
}

X* x;

try
{
x = new X; // is 'x' a complete object?
}
catch (...)
{
delete x; // is this valid?
}
Is it only the throw() that determines if an object has been constructed or
not?
Jun 27 '08 #1
2 1384
barcaroller wrote:
At which point is an object considered to be completely constructed?

X::X()
{
// constructor stuff

throw "Exception"; // premature exit
}

X* x;

try
{
x = new X; // is 'x' a complete object?
}
catch (...)
{
delete x; // is this valid?
}
Is it only the throw() that determines if an object has been constructed or
not?

IIRC it's considered as completely constructed when the constructor
exits (successfully).

In this case the delete will give undefined behaviour, because even if
I'm subtly wrong about when "construction is complete" you'll never get
to the return from the new call after which the assignment to x is made
- so x will have an undefined value.

I get nervous about exceptions in constructors...

Andy
Jun 27 '08 #2
On May 10, 10:24 am, Andy Champ <no....@nospam.comwrote:
barcaroller wrote:
At which point is an object considered to be completely constructed?
X::X()
{
// constructor stuff
throw "Exception"; // premature exit
}
X* x;
x must be initialized; otherwise its use is undefined behavior:

X * x = NULL;
try
{
x = new X; // is 'x' a complete object?
}
catch (...)
{
delete x; // is this valid?
}
Is it only the throw() that determines if an object has been constructed or
not?

IIRC it's considered as completely constructed when the constructor
exits (successfully).
That's correct.
In this case the delete will give undefined behaviour, because even if
I'm subtly wrong about when "construction is complete" you'll never get
to the return from the new call after which the assignment to x is made
- so x will have an undefined value.
Correct.
I get nervous about exceptions in constructors...
Yet there is only exceptions to avoid a half constructed object.

Herb Sutter's Exceptional C++ has been the book that eliminated my
nervousness in the past. The book presents a number of guidelines
which results in code that works even if exceptions are thrown.

One such guideline is to never release resources explicitly in code
(the idiom is RAII and predates Herb Sutter's book). so the original
code better be

SomeSmartPointer x(new X());

Ali
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony | last post by:
Hi, Can anyone help me out here, I'm looking for a design (pattern?) for a-synchronic construction of a class. I need this design in a framework which will run in a multithreaded system. I...
4
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
0
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
1
by: ChasW | last post by:
Im looking for some technical clarification regarding exactly what the differences, if any, are between the two pieces of code are, aside from the obvious syntactical differences. int x = 5; ...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.