473,498 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10424
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
21169
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
4724
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
2967
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
2356
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
6524
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
5787
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
4659
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
15865
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
3626
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
3691
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
7004
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
7208
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...
1
6890
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
7379
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
5464
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,...
0
3095
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...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
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...

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.