Constructor

M

Mike Wahler

giles said:
Hi,

Does a compiler generated constructor initialise the data members?

Only those which have default constructors. This of
course does not include the built-in types.

So, the answer depends upon the data member(s) type(s).

-Mike
 
M

Mike Wahler

giles said:
"Mike Wahler" <[email protected]> wrote in message

Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?

That's the same thing. Before the ctor is invoked, memory for
the object is alloted (either as a static or automatic object, or
dynamically allocated). Then the constructor is called, which
'constructs' (a.k.a. initializes) the object.
When a contructor throws an exception the destructor for it is not
called. Why is this?

Because if the constructor does not complete, then the
object has not been constructed. You don't need a destructor
called for an object which hasn't been created.

However, if you allocate your object with operator 'new'
and the ctor throws, then the compiler is responsible for
ensuring that the allocated memory is freed. Note that
there's still no destructor call. A destructor is only
for 'cleaning up' a fully constructed object. An object
whose contstructor has thrown an exception is not such
an object.
The object is created in the memory,

Memory space is made for the object when it is allocated
or defined. No real 'object' exists yet at that point,
only 'raw' memory. It's the constructor that actually
'builds' ('creates') the object in that memory, i.e.
initializes the data members.
its just
that its not initialised

Right. There's no 'object' yet, only 'raw' memory.
so why shouldn't the destructor get called

Because there's nothing to destruct. :)
and how does the compiler know it shouldn't call the destructor?

The language definition dicates that. A compiler that
conforms to the language will 'know'.

-Mike
 
O

osmium

giles said:
Does a compiler generated constructor initialise the data members?

No, the data elements will have whatever random bits they had from some
previous use.
 
G

giles

Mike Wahler said:
Only those which have default constructors. This of
course does not include the built-in types.

So, the answer depends upon the data member(s) type(s).

-Mike

Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?
When a contructor throws an exception the destructor for it is not
called. Why is this? The object is created in the memory, its just
that its not initialised so why shouldn't the destructor get called
and how does the compiler know it shouldn't call the destructor?

Giles
 
R

Rob Williscroft

giles wrote in
Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?

Assuming you mean "allocating the memory for the object" when you say
"create it in the memory" then yes.
When a contructor throws an exception the destructor for it is not
called. Why is this? The object is created in the memory, its just
that its not initialised so why shouldn't the destructor get called

The answer is in you question, like the constructor isn't responsible
for "creating it in the memory". The destructor isn't responsable for
deallocating the object's memory. All it does is uninitialise it,
since the constructor didn't complete the object's memory is still
uninitialised so the destructor call isn't required.

Note that when you write:

X *x = new X;

and X's constructor throw's, all though the compiler doesn't have to
arrange for X's destructor to be called it does have to arrange for
operator delete( x ); to be called, to deallocate the objects memory.
and how does the compiler know it shouldn't call the destructor?

Because its a computer programme and its been written that way (*).

(*) I'm just guessing :).

Rob.
 
J

Jonathan Mcdougall

I am not sure when an object is created using 'new', compiler would
arrange for the memory to be de-allocated. It should be a memory leak.

That is the way the language works. If the ctor throws, no matter if
it was allocated with new or not, no memory is allocated.


Jonathan
 

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

Forum statistics

Threads
474,138
Messages
2,570,804
Members
47,349
Latest member
jojonoy597

Latest Threads

Top