473,465 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Default constructor weirdness in member initialization list

struct S1
{
int i;
};

struct S2 {
S1 s;
// version 1
S2() {} ;
// version 2
S2() : s() {} ;
};

int main()
{
S2 s;
printf( "%d\n", s.s.i );
return 0;
}

----------

With version 1 of the S2 constructor the program will spew random
values as expected.
With version 2 of the S2 constructor the program only spews zeros.
Does explicitly calling the default constructor in the member
initialization list somehow cause the "default" constructor for the
int in S1 to be called?
Feb 1 '08 #1
3 2425
On Feb 1, 5:18 am, mhvau...@gmail.com wrote:
struct S1
{
int i;

};

struct S2 {
S1 s;
// version 1
S2() {} ;
// version 2
S2() : s() {} ;

};

int main()
{
S2 s;
printf( "%d\n", s.s.i );
return 0;

}

----------

With version 1 of the S2 constructor the program will spew random
values as expected.
With version 2 of the S2 constructor the program only spews zeros.
Does explicitly calling the default constructor in the member
initialization list somehow cause the "default" constructor for the
int in S1 to be called?
Hi
I tried the program with g++ it works same both ways, as should. Which
compiler did you use?

cheers
projyal
Feb 1 '08 #2
On Jan 31, 5:22 pm, devp <proj...@gmail.comwrote:
On Feb 1, 5:18 am, mhvau...@gmail.com wrote:
struct S1
{
int i;
};
struct S2 {
S1 s;
// version 1
S2() {} ;
// version 2
S2() : s() {} ;
};
int main()
{
S2 s;
printf( "%d\n", s.s.i );
return 0;
}
----------
With version 1 of the S2 constructor the program will spew random
values as expected.
With version 2 of the S2 constructor the program only spews zeros.
Does explicitly calling the default constructor in the member
initialization list somehow cause the "default" constructor for the
int in S1 to be called?

Hi
I tried the program with g++ it works same both ways, as should. Which
compiler did you use?
[615]$g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c+
+,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-
system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-
threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/
4.1.3 --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --enable-checking=release
x86_64-linux-gnu
Thread model: posix
gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

>
cheers
projyal
Feb 1 '08 #3
Alf P. Steinbach wrote:
...
>In C++ there's no syntax and way in general to "explicitly call" a
constructor.

I'm sorry, that's incorrect.

There's no dedicated C++ syntax for constructor calls. What the syntax
means depends on context. Whether to say that an explicit constructor
call is an explicit constructor call is a terminologial issue.
I disagree. Constructors are functions. They are special functions, but
functions nevertheless. For this reason I'd expect the "explicit
constructor call", if it existed, to be have the function call
semantics. However, if I'm not mistaken, there's no way to call
constructor in C++ by using function call semantics. Constructors calls
in C++ are "side effects" of the more generic process called
"initialization". It is is invoked by various language constructs
having... well, initialization semantics, which is essentially different
from function call semantics. This is why I say that there's no way to
"call" a constructor in C++.
The language's creator calls an explicit constructor call an explicit
constructor call.
Where? In TC++PL? If yes, then sorry, but that book is not canonical in
the context of ANSI standard C++ discussion. It is intentionally
simplified to be more accessible for newcomers. And the price of that
simplification is that sometimes it makes statements, which are simply
incorrect from the standard C++ point of view. (For example, it states
that scalar types have constructors, if I remember correctly).

--
Best regards,
Andrey Tarasevich
Feb 1 '08 #4

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

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
9
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument....
16
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){...
9
by: romayankin | last post by:
Here is the problem. There are two constructors of the same class ~~~~~~~~~~~~ CClass() : param1 (0.5), param2 (100000), param3 (NULL), szPath (NULL) { }
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,
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...
6
by: Mark | last post by:
I have a problem with a template class defined: // start matrix.h file template <class Tclass Matrix { public: Matrix() { // default constructor } Matrix(const Subscript rows, const...
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...
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
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
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,...
1
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,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.