Hi,
Does a compiler generated constructor initialise the data members?
Giles 6 3019
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.
giles <gi************ ***@yahoo.co.in > wrote in message
news:2a******** *************** ***@posting.goo gle.com... 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
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<bg******* ***@slb6.atl.mi ndspring.net>.. . giles <gi************ ***@yahoo.co.in > wrote in message news:2a******** *************** ***@posting.goo gle.com... 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
giles wrote in news:2a******** *************** ***@posting.goo gle.com: 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/
giles <gi************ ***@yahoo.co.in > wrote in message
news:2a******** *************** ***@posting.goo gle.com... "Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:<bg******* ***@slb6.atl.mi ndspring.net>.. . giles <gi************ ***@yahoo.co.in > wrote in message news:2a******** *************** ***@posting.goo gle.com... 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jun |
last post by:
I have following script
<script>
var Animal = function(name){
this.name = name;
}
Animal.prototype.eat = function (food)
{
|
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 pointers) and for objects types call
their default constructor.
Any others points i should know?
|
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) .
|
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() {}
};
|
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 constructor that all parameters have default values
| |
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 class, if the
class has more than than one cosntructor, by making sure that each
constructor has a different signature. I have managed to learn that and get
|
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 parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
|
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)
{}
|
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 creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
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");
}
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |