473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Initialisation


I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations .

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation" , as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

--

Frederick Gotham
Jul 20 '06 #1
13 2059
Frederick Gotham wrote:
I have just been reading 8.5 in the Standard, and am trying to make
sense of the different kinds of initialisations .

Up until now, I thought of an object as either NOT being initialised
(i.e. containing garbage), or being default initialised (i.e.
containing the default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
And there is the fourth kind: uninitialised.
In my above code snippet, what kind of initialisation am I peforming?
In the first part, none. Objects are _uninitialised_ . In the second
part the _default_.
I think it's "default initialisation" , as I'm giving the object its
default value.
Right. Only in the second case.
What is zero-initialisation, and can anyone give an example of it
please?
That's the incarnation of the default-initialisation for PODs.
What is value-initialisation, and can anyone give an example of it
please?
int a(5);
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};
Default.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #2
Victor Bazarov posted:

>What is zero-initialisation, and can anyone give an example of it
please?

That's the incarnation of the default-initialisation for PODs.

The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation" .

Are they not exactly the same? If so, why give them different names?

Can you please give an example of zero-initialising an object?

In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
MyStruct static obj = {}; /* zero or default? */
MyStruct obj = {}; /* zero or default? */
}

--

Frederick Gotham
Jul 20 '06 #3
Frederick Gotham wrote:
Victor Bazarov posted:

>>What is zero-initialisation, and can anyone give an example of it
please?

That's the incarnation of the default-initialisation for PODs.


The reason we invent words is not so we have a label for something,
but so that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation" .

Are they not exactly the same?
No. Yes. For PODs they are the same. For non-PODs they are not
the same.
If so, why give them different names?
Because they are not, in general case.
Can you please give an example of zero-initialising an object?
static int a;
void *p(0);
In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
Zero.
MyStruct static obj = {}; /* zero or default? */
First zero, then default (which in this case superfluous).
MyStruct obj = {}; /* zero or default? */
Default, which for POD is zero-.
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #4
Victor Bazarov posted:
No. Yes. For PODs they are the same. For non-PODs they are not
the same.

Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?
All the above initialisations are equivalent to:
int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();
I still don't understand how zero-initialisation is any different to
default-initialisation. . .
--

Frederick Gotham
Jul 20 '06 #5
Frederick Gotham wrote:
Victor Bazarov posted:
>No. Yes. For PODs they are the same. For non-PODs they are not
the same.


Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?
Sort of. '0' is an octal int literal, intialising a 'double' with it
potentially involves a temporary of type 'double' and initialising
a pointer with it potentially involves a temporary null pointer. The
compiler probably does everything behind the scenes and during compile-
time, anyway.
All the above initialisations are equivalent to:
int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();
Right.
I still don't understand how zero-initialisation is any different to
default-initialisation. . .
For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #6

Frederick Gotham wrote:
I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations .

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation" , as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

--

Frederick Gotham
Hello Frederick,
I tried your code in VC++ 6.0. It throws compilation
errors.

All the errors point to the code MyStruct obj = {};

\Initialization .cpp(49) : error C2059: syntax error : '}'
\Initialization .cpp(49) : warning C4508: 'main' : function should
return a value; 'void' return type assumed
\Initialization .cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization .cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization .cpp(53) : error C2143: syntax error : missing ';'
before '}'

What compiler are you using?

All,
Is it correct way to initialize the struct like this?

Thanks
Sabiyur

Jul 20 '06 #7
Sabiyur wrote:
Frederick Gotham wrote:
>[..]
Frederick Gotham

Hello Frederick,
I tried your code in VC++ 6.0. [..]
Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.
What compiler are you using?

All,
Is it correct way to initialize the struct like this?
Of course. Get a decent compiler. VC++ 2005 EE is available for
free from MS. No comparison, so much better standard compliance.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #8
Victor Bazarov posted:
>I still don't understand how zero-initialisation is any different to
default-initialisation. . .

For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.

I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};
So what's zero initialisation?
--

Frederick Gotham
Jul 20 '06 #9
Frederick Gotham wrote:
Victor Bazarov posted:
>>I still don't understand how zero-initialisation is any different to
default-initialisation. . .

For non-POD it involves calling the default c-tor. There are no
c-tors for PODs, and that's why there is no difference between
'default-' and 'zero-' for PODs. Just read the Standard.


I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};
So what's zero initialisation?
Zero-initialisation is what happens with the POD when you
default-initialise it. So, the last statement is zero-initialisation
(*by definition* of the default-initialisation for PODs). Which part
of it do you not understand?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 20 '06 #10

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

Similar topics

18
3030
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less...
12
5462
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include...
106
5520
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor. My question is when to not use an...
1
1466
by: Tony Johansson | last post by:
Hello!! Assume I have a wrapper class with this definition. class Integer { public: Integer(int =0); int get() const; Integer& set(int); Integer& add(const Integer&);
11
1540
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the question into the following code: ===code starts=== #include <iostream> using namespace std;
10
2360
by: Tom the Canuck | last post by:
What would be the best way to proceed? Should I make a pure virtual class and then derive from that? I want the base class to have functions defined so that I don't have to do the work all over again in the derived class. Is this not what objects are for? Please illuminate on how to do this properly. I don't want code, just a pointer on how...
2
2073
by: Christoph Conrad | last post by:
Hi, given the following test case, with CString from Microsofts MFC: ================================================== file 1: CString arr; ==================================================
14
2563
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
2
1247
by: Brett_McS | last post by:
Fairly new to Python (and loving it!) In C++ (gack!) I got used to creating a helper function with each class to check the class object initialisation parameters prior to creating the object. In Python, this would be ----------------------------------------------- import example
0
8215
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, 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. ...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 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...
0
6626
agi2029
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...
1
5718
isladogs
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...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.