niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
does the default constructor initialize values?
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
Author Message
NewToCPP
*nix forums beginner


Joined: 08 Dec 2005
Posts: 31

PostPosted: Thu Jul 20, 2006 3:37 pm    Post subject: does the default constructor initialize values? Reply with quote

does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?

thanks.
Back to top
roberts.noah@gmail.com
*nix forums Guru


Joined: 11 Sep 2005
Posts: 795

PostPosted: Thu Jul 20, 2006 3:40 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP wrote:
Quote:
does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?

It zero initializes them.

Quote:

thanks.
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jul 20, 2006 3:44 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

* Noah Roberts:
Quote:
NewToCPP wrote:
does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?

It zero initializes them.

No, it does exactly nothing (in this class).

int main()
{
A o; // Default-constructed, members of o are not initialized.
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Rolf Magnus
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1236

PostPosted: Thu Jul 20, 2006 3:46 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP wrote:

Quote:
does the default constructor initialize values?

That depends.

Quote:
I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};

Note that this class is quite useless, because all the members are private.

Quote:
Does the default constructor initialize the values of i, c & iPtr?

In this case, the default constructor will be generated by the compiler. It
does nothing, so no, the values won't get initialized.
Back to top
roberts.noah@gmail.com
*nix forums Guru


Joined: 11 Sep 2005
Posts: 795

PostPosted: Thu Jul 20, 2006 3:52 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

Alf P. Steinbach wrote:
Quote:
* Noah Roberts:
NewToCPP wrote:
does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?

It zero initializes them.

No, it does exactly nothing (in this class).

int main()
{
A o; // Default-constructed, members of o are not initialized.
}

That doesn't actually call the default constructor of a POD though.

A o = A();
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Thu Jul 20, 2006 4:36 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP posted:

Quote:
class A
{

int i;
char c;
int * iPtr;

};


That is equivalent to:

struct A {
private:

int i;
char c;
int *p;
};


Let's remove the "private" as it just complicates the example. So we have:

struct A {

int i;
char c;
int *p;
};

int main()
{
A obj1; /* Contains garbage */

A obj2 = {}; /* Each member gets its default value */

A obj3 = A(); /* Each member gets its default value */

A static obj4; /* Each member gets its default value */
}



--

Frederick Gotham
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jul 20, 2006 6:01 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

* Noah Roberts:
Quote:
Alf P. Steinbach wrote:
* Noah Roberts:
NewToCPP wrote:
does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?
It zero initializes them.
No, it does exactly nothing (in this class).

int main()
{
A o; // Default-constructed, members of o are not initialized.
}

That doesn't actually call the default constructor of a POD though.

A o = A();

On the contrary. What you have here is copy construction from a
zero-initialized instance (the expression 'A()' means
default-initialization, 8.5/7, which for a POD is defined as
zero-initialization, 8.5/5). That's not to say that 'A o;' necessarily
actually calls a default constructor: it doesn't affect the outcome
whether you say that there is no default constructor for a POD class (no
call), or that the default constructor does absolutely nothing (call
does nothing), but the standard specifies an implicitly declared default
constructor, 12.1/5, which if it actually exists must do nothing at all.

In particular, if you derive from class A,

struct A { /* as before */ };

struct B: A { int x; int y; B(): x(1), y(2) {} };

B o;

then you have an object 'o' with x and y initialized, and i, c, and iPtr
uninitialized. If class A had a default constructor that did
zero-initialization, then instead you would have i, c and iPtr zeroed.
But that isn't what you get, so the notion of a zeroing automatically
generated default constructor leads to incorrect, dangerous conclusions.

However, with a /conforming/ compiler,

struct B: A { int x; int y; B(): x(1), y(2), A() {} };

should ensure zero-initializion of the A base class sub-object; for a
POD this syntax does not denote a call of the (possibly existing or not)
default constructor, it instead denotes default initialization which is
defined as zero-initialization (references given above).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
NewToCPP
*nix forums beginner


Joined: 08 Dec 2005
Posts: 31

PostPosted: Thu Jul 20, 2006 7:23 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

Does it initialize if the members are public?


Rolf Magnus wrote:
Quote:
NewToCPP wrote:

does the default constructor initialize values?

That depends.

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};

Note that this class is quite useless, because all the members are private.

Does the default constructor initialize the values of i, c & iPtr?

In this case, the default constructor will be generated by the compiler. It
does nothing, so no, the values won't get initialized.
Back to top
NewToCPP
*nix forums beginner


Joined: 08 Dec 2005
Posts: 31

PostPosted: Thu Jul 20, 2006 7:25 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

Frederick:

What are the default values for each of those members?

I guess for "i ==> 0", "iPtr ==> NULL" what is default for c?



Frederick Gotham wrote:
Quote:
NewToCPP posted:

class A
{

int i;
char c;
int * iPtr;

};


That is equivalent to:

struct A {
private:

int i;
char c;
int *p;
};


Let's remove the "private" as it just complicates the example. So we have:

struct A {

int i;
char c;
int *p;
};

int main()
{
A obj1; /* Contains garbage */

A obj2 = {}; /* Each member gets its default value */

A obj3 = A(); /* Each member gets its default value */

A static obj4; /* Each member gets its default value */
}



--

Frederick Gotham
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Thu Jul 20, 2006 7:46 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP posted:

Quote:
Frederick:

What are the default values for each of those members?

I guess for "i ==> 0", "iPtr ==> NULL" what is default for c?


Numeric types, e.g. integer types and floating point types, get 0.

("char" is a numeric type.)


"bool" becomes false.


Pointers get the null pointer value.


In all cases, it's as if you'd written:

IntrinsicType object = 0;



--

Frederick Gotham
Back to top
NewToCPP
*nix forums beginner


Joined: 08 Dec 2005
Posts: 31

PostPosted: Thu Jul 20, 2006 10:21 pm    Post subject: Re: does the default constructor initialize values? Reply with quote

What does it do in case of non-public members?


Frederick Gotham wrote:
Quote:
NewToCPP posted:

Frederick:

What are the default values for each of those members?

I guess for "i ==> 0", "iPtr ==> NULL" what is default for c?


Numeric types, e.g. integer types and floating point types, get 0.

("char" is a numeric type.)


"bool" becomes false.


Pointers get the null pointer value.


In all cases, it's as if you'd written:

IntrinsicType object = 0;



--

Frederick Gotham
Back to top
Alan Johnson
*nix forums Guru Wannabe


Joined: 14 May 2005
Posts: 206

PostPosted: Fri Jul 21, 2006 2:51 am    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP wrote:

Quote:
Rolf Magnus wrote:
NewToCPP wrote:

does the default constructor initialize values?
That depends.

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};
Note that this class is quite useless, because all the members are private.

Does the default constructor initialize the values of i, c & iPtr?
In this case, the default constructor will be generated by the compiler. It
does nothing, so no, the values won't get initialized.

Does it initialize if the members are public?

No.

Also, look at the fourth bullet point in this FAQ:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4

--
Alan Johnson
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Fri Jul 21, 2006 3:30 am    Post subject: Re: does the default constructor initialize values? Reply with quote

NewToCPP posted:

Quote:
What does it do in case of non-public members?


Everything's the same, regardless of whether a member is public, private or
protected.


--

Frederick Gotham
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:42 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts change default install directory when using bdist_rpm krithika.sridhar@gmail.co python 1 Fri Jul 21, 2006 4:09 am
No new posts Question about using copy constructor of parent class? LJB C++ 4 Thu Jul 20, 2006 9:32 pm
No new posts converting array values to monomaniac21 PHP 11 Thu Jul 20, 2006 10:17 am
No new posts default route entry is missing. Rijesh Tru64 managers mail-list 0 Thu Jul 20, 2006 9:48 am
No new posts Must GET/POST Parameters Have Values? (And What is the Sy... David T. Ashley PHP 2 Wed Jul 19, 2006 1:10 am

Loans | Credit Cards | Debt Help | Xbox Mod Chip | Xecuter 3 Mod Chip
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2017s ][ Queries: 16 (0.0891s) ][ GZIP on - Debug on ]