473,385 Members | 1,872 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,385 software developers and data experts.

Constructor

Hi,

Does a compiler generated constructor initialise the data members?

Giles
Jul 19 '05 #1
6 2991
giles writes:
Does a compiler generated constructor initialise the data members?


No, the data elements will have whatever random bits they had from some
previous use.
Jul 19 '05 #2

giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
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

Jul 19 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bg**********@slb6.atl.mindspring.net>...
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
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


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
Jul 19 '05 #4
giles wrote in news:2a**************************@posting.google.c om:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bg**********@slb6.atl.mindspring.net>...
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
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


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

Jul 19 '05 #6
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?


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

Jul 19 '05 #7

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

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.