473,398 Members | 2,404 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,398 software developers and data experts.

does the default constructor initialize values?

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.

Jul 20 '06 #1
12 5782

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.
>
thanks.
Jul 20 '06 #2
* 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.
}

--
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?
Jul 20 '06 #3
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.

Jul 20 '06 #4

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();

Jul 20 '06 #5
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
Jul 20 '06 #6
* Noah Roberts:
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?
Jul 20 '06 #7
Does it initialize if the members are public?
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.
Jul 20 '06 #8
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:
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
Jul 20 '06 #9
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
Jul 20 '06 #10
What does it do in case of non-public members?
Frederick Gotham wrote:
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
Jul 20 '06 #11
NewToCPP wrote:
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-lit...t.html#faq-5.4

--
Alan Johnson
Jul 21 '06 #12
NewToCPP posted:
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
Jul 21 '06 #13

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

Similar topics

29
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default...
3
by: swengtoo | last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to "Avoid gratuitous default constructors". To summarize his claims against default constructors for "the right classes" he...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
3
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
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...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Macneed | last post by:
i am a newbie, i remember i read a book talking about when u declare a array variable using float ABC = new float; the whole array element in ABC ( ABC to ABC ) will automatic initialize to 0...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...
0
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,...

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.