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

Default constructor

class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?
Jul 19 '05 #1
14 10414
Vinodh Kumar wrote:
class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?


Type 'int' is not a class type and it has no constructor. There's
nothing to invoke.

If a subobject of POD type is not mentioned in constructor's member
initializer list, it is not initialized at all. See 12.6.2/4 in the
language standard.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #2
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...

Does *int* has no non-trivial default constructor?


There can be at most one default constructor. It's the one that's called by
default. On some occasions to initialize an int where it pops into existence
is just a waste of time, e.g.,

int k;
if(some_condition)
{ do_something(); k = 2;
}
else
{ do_something_else(); k = 3;
}

To give k an initial value here might be considered good practice, but it
shouldn't be mandatory because it just wastes time and space. I suppose it
is for this reason, and perhaps for compatibility with C as well, that there
is no default constructor for int. The same goes for an int class member.
It's up to you to initialize it explicitly if you want it to have a specific
value.

However, int does have a non-default constructor that takes no arguments:

int k = int(); // = 0

DW

Jul 19 '05 #3
Vinodh Kumar wrote:
> The value of *myIntVal* in *a* object is indetereminate.
> The default constructor synthesized by the compiler, does not
> invoke the constructor for the *int*.
> Why?


Type 'int' is not a class type and it has no constructor. There's
nothing to invoke.

If a subobject of POD type is not mentioned in constructor's member
initializer list, it is not initialized at all. See 12.6.2/4 in the
language standard.

OK.

Does *int* has no non-trivial default constructor?


Type 'int' is not a class type and it has no constructor (copy/paste
from above). No trivial or non-tivial constructor, no default
constructor, not any constructor at all. A concept of constructors only
exists for class types in C++.

Jul 19 '05 #4

Vinodh Kumar <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?


Because built-in types do not have constructors.

Which C++ book(s) are you reading?

-Mike

Jul 19 '05 #5

Vinodh Kumar <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:3F**************@hotmail.com...
Vinodh Kumar wrote:
class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the constructor for the *int*.
Why?


Type 'int' is not a class type and it has no constructor. There's
nothing to invoke.

If a subobject of POD type is not mentioned in constructor's member
initializer list, it is not initialized at all. See 12.6.2/4 in the
language standard.

OK.

Does *int* has no non-trivial default constructor?


*None* of the built-in types (which include 'int') have
constructors at all. Only classes and structs can have
constructors.

-Mike

Jul 19 '05 #6
Mike Wahler wrote:
...
class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?


Because built-in types do not have constructors.

Which C++ book(s) are you reading?
...


It might by surprising to many people here to find out that this
misunderstanding often originates from TC++PL book. For example, section
10.4.2 in 3rd edition of TC++PL ends with the following sentence:

"Built-in types also have default constructors".

And in many other places in the book there are references to
constructors of built-in types. I'm sure Bjarne Stroustrup had his
reasons to use an alternative non-standard terminology (most likely in
order to simplify things a bit within the context of the book). The
unpleasant side effect of this is that readers of TC++PL sometimes feel
lost and confused in C++ community that prefers the standard terminology.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #7
Mike Wahler <mk******@mkwahler.net> wrote in message
news:bg**********@slb6.atl.mindspring.net...
However, int does have a non-default constructor


No it does not. Built-in types don't have constructors.
that takes no arguments:

int k = int(); // = 0


This syntax (introduced for 'symmetry') makes it *look*
like there's a ctor, but there's not.


My information comes from page 131 of "The C++ Programming Language". I
quote:

"The value of an explicit use of the constructor for a built-in type is 0
converted to that type. Thus, int() is another way of writing 0."

DW

Jul 19 '05 #8
David White wrote:
...
> However, int does have a non-default constructor


No it does not. Built-in types don't have constructors.
> that takes no arguments:
>
> int k = int(); // = 0


This syntax (introduced for 'symmetry') makes it *look*
like there's a ctor, but there's not.


My information comes from page 131 of "The C++ Programming Language". I
quote:

"The value of an explicit use of the constructor for a built-in type is 0
converted to that type. Thus, int() is another way of writing 0."
...


Just as I said in another message in thus thread, Bjarne Stroustrup
makes use of some non-standard terminology in his TC++PL book. There is
no point in arguing about this. According to TC++PL, built-in types do
have constructors. According to the standard, built-in types have no
constructors at all.

Nevertheless, both the standard and TC++PL indicate that member
subobjects of built-in types are treated differently from non-POD class
types with regard to initialization. In particular, both sources
indicate that the member subobject from the OP's original message will
be left uninitialized.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #9
Andrey Tarasevich <an**************@hotmail.com> wrote in message
news:3F**************@hotmail.com...
David White wrote:
My information comes from page 131 of "The C++ Programming Language". I
quote:

"The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0."
...


Just as I said in another message in thus thread, Bjarne Stroustrup
makes use of some non-standard terminology in his TC++PL book. There is
no point in arguing about this. According to TC++PL, built-in types do
have constructors. According to the standard, built-in types have no
constructors at all.


In what circumstance does it matter? I accept that built-in types do not
have _default_ constructors (and I said that in my first post), but to argue
whether they do or don't have constructors at all seems a pointless argument
over definitions, unless there is some circumstance in C++ code in which the
distinction is important.

int j(3);
int k = int(5);

If it looks like a constructor and quacks like a constructor, then for all
intents and purposes it _is_ a constructor, isn't it?

DW

Jul 19 '05 #10
Jim Fischer wrote:
...
My information comes from page 131 of "The C++ Programming Language". I
quote:

"The value of an explicit use of the constructor for a built-in type is 0
converted to that type. Thus, int() is another way of writing 0."


IMO, this is an unfortunate choice of wording. C++'s built in types
(a.k.a., "plain old data" [POD] types) are NOT implemented as classes.
Since class types are the only types that have constructors, and since
POD types are not classes, then POD types do not have constructors.

FWIW, I suspect the meaning of the sentences on page 131 is something
more like,

The value of an explicit use of constructor notation with no
argument for a built-in type is 0 converted to that type.
Thus, int() is another way of writing 0."

or even,

The value of an explicit use of functional notation with no
argument for a built-in type ..."
...


That definitely is how these sentences would sound in standard wording.
But I don't think that the original form is nothing more than an
unfortunate choice of wording. For example, a sentence on page 224
(10.4.2) of the 3rd edition is more explicit and less open to
interpretations:

"Built-in types also have default constructors".

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #11
David White wrote:
...
Just as I said in another message in thus thread, Bjarne Stroustrup
makes use of some non-standard terminology in his TC++PL book. There is
no point in arguing about this. According to TC++PL, built-in types do
have constructors. According to the standard, built-in types have no
constructors at all.


In what circumstance does it matter? I accept that built-in types do not
have _default_ constructors (and I said that in my first post), but to argue
whether they do or don't have constructors at all seems a pointless argument
over definitions, unless there is some circumstance in C++ code in which the
distinction is important.

int j(3);
int k = int(5);

If it looks like a constructor and quacks like a constructor, then for all
intents and purposes it _is_ a constructor, isn't it?
...


This form - maybe, but not the '()' form, which is supposed to refer to
default constructor. It doesn't really quack like a constructor in all
contexts. For example, automatic objects and member subobjects of
built-in types are not initialized, unless an explicit initializer is
specified by the user. This wouldn't be the case if built-in types had
default constructors, which would quack when default constructors are
supposed to quack.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #12
Andrey Tarasevich <an**************@hotmail.com> wrote in message
news:3F**************@hotmail.com...
int j(3);
int k = int(5);

If it looks like a constructor and quacks like a constructor, then for all intents and purposes it _is_ a constructor, isn't it?
...


This form - maybe, but not the '()' form, which is supposed to refer to
default constructor. It doesn't really quack like a constructor in all
contexts. For example, automatic objects and member subobjects of
built-in types are not initialized, unless an explicit initializer is
specified by the user. This wouldn't be the case if built-in types had
default constructors, which would quack when default constructors are
supposed to quack.


Quack(); /* :-) */

-Mike

Jul 19 '05 #13
int i = int(5);

In the above statement *int(5)* is not a constructor invocation?
Is there any guideline in the standard saying that primary types should not
be implemented as classes?
What is the typical implemenation of *int(5)* in some of the famous, good
implementations?
I know nothing is perfect.
But whats the truth?
The truth is that then we can't guarantee the C++ atomic types are as good
as their "C' counterparts, if we are implementing *int* as classes(As one is
always less than two in positive logic).
Right?
So my intution is ( since i have never seen any sourcecode of any c++
compilers) that all the available typical implementations of C++ provide
*int(5)* as a mere synctaical resemblance of nonPODs.
Thanks for all.

Regards,
Vinodh Kumar P

"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
class A
{
int myIntVal;
};

int main()
{
A a;
return 0;
}

The value of *myIntVal* in *a* object is indetereminate.
The default constructor synthesized by the compiler, does not invoke the
constructor for the *int*.
Why?

Jul 19 '05 #14
Vinodh Kumar wrote:
int i = int(5);

In the above statement *int(5)* is not a constructor invocation?
Right, it's not.
Is there any guideline in the standard saying that primary types
should not be implemented as classes?
Yep

3.9.1:
There are two kinds of types: fundamental types and compound types.

The descriptions of "fundamental types" and "compound types" are a bit
long, but basically, the types you call "primary types" are the
"fundamental types". Classes are compound types.
What is the typical implemenation of *int(5)* in some of the famous,
good implementations?
What do you mean? It does the same as (int)5.
I know nothing is perfect.
But whats the truth?
The truth is that then we can't guarantee the C++ atomic types are as
good as their "C' counterparts, if we are implementing *int* as
classes
Well, if it were a POD class, we could in theory. But POD classes may
not have user-defined constructors :-)
(As one is always less than two in positive logic).
Er... what?
Right?
So my intution is ( since i have never seen any sourcecode of any c++
compilers) that all the available typical implementations of C++
provide *int(5)* as a mere synctaical resemblance of nonPODs.


That's right. Having the same syntax for user-defined types and builtin
types can be useful in templates, so it's not only syntactic sugar.

Jul 19 '05 #15

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

Similar topics

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...
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...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
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: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.