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

initialization within constructor

Hi everyone,

Could someone tell us why the below code compiles and works? Is it
legal in the constructor of class B?

Cheers

Ronny

---------------------------------

#include <iostream>

using namespace std;

class a {
int x;
public:
void show() { cout<<x<<endl; }
a(int xx) : x(xx) {}
a() {}
};

class B {
a ma;
public:
B(int xx);
void disp() { ma.show(); }
};

B::B(int xx) {
ma = a(xx);
}
int main() {
B b(3);
b.disp();

return 0;
}

Jun 14 '06 #1
6 2463
rona wrote:
Could someone tell us why the below code compiles and works?
Probably because it's well-formed...
Is it
legal in the constructor of class B?
Yes, why do you ask? What throws you off?
---------------------------------

#include <iostream>

using namespace std;

class a {
int x;
public:
void show() { cout<<x<<endl; }
a(int xx) : x(xx) {}
a() {}
};

class B {
a ma;
public:
B(int xx);
void disp() { ma.show(); }
};

B::B(int xx) {
ma = a(xx);
This is an assignment from a temporary 'a' object (which is in turn
initialised from the 'xx') to the member 'ma'.
}
You could achieve the same effect by doing

B::B(int xx) {
ma = xx;
}

or, even better, by

B::B(int xx) : ma(xx) {
}


int main() {
B b(3);
b.disp();

return 0;
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #2
rona wrote:
Hi everyone,

Could someone tell us why the below code compiles
Yes, because there are no compile-time errors.
and works?
That's harder to determine. What are the requirements for the program?
Is it
legal in the constructor of class B?
Yes. See below.

Cheers

Ronny

---------------------------------

#include <iostream>

using namespace std;

class a {
int x;
public:
void show() { cout<<x<<endl; }
a(int xx) : x(xx) {}
a() {}
This is bad because x is left unitialized and could remain so if the
user isn't careful. You probably added it so class B would compile.
};

class B {
a ma;
public:
B(int xx);
void disp() { ma.show(); }
};

B::B(int xx) {
ma = a(xx);
}


This is legal, but you should delete the default constructor for class
a and use an initialization list here (see
http://www.parashift.com/c++-faq-lit...html#faq-10.6). It works
because there is an implicit = operator generated for class a, and you
"convert" the integer xx to an "a" object by creating a temporary
object of type a. (Actually, it would also work if the line read "ma =
xx;" since that same constructor would be called implicitly.)

Cheers! --M

Jun 14 '06 #3
Victor Bazarov wrote:
rona wrote:
Could someone tell us why the below code compiles and works?
Probably because it's well-formed...
Is it
legal in the constructor of class B?


Yes, why do you ask? What throws you off?


Couple of reasons:
1- I knew using a constructor initializer list was the best option.
Eckel's book sounded as if it was the ONLY option. My mistake.
2- Still confused about what happens to the initial storage allocated
to the member ma. When ma is first declared within the class definition
for B, is memory allocated to ma. If yes, what happens to this memory
after "ma = a(xx);" ?

Cheers
---------------------------------

#include <iostream>

using namespace std;

class a {
int x;
public:
void show() { cout<<x<<endl; }
a(int xx) : x(xx) {}
a() {}
};

class B {
a ma;
public:
B(int xx);
void disp() { ma.show(); }
};

B::B(int xx) {
ma = a(xx);


This is an assignment from a temporary 'a' object (which is in turn
initialised from the 'xx') to the member 'ma'.
}


You could achieve the same effect by doing

B::B(int xx) {
ma = xx;
}

or, even better, by

B::B(int xx) : ma(xx) {
}


int main() {
B b(3);
b.disp();

return 0;
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 14 '06 #4
rona wrote:
Victor Bazarov wrote:
rona wrote:
Could someone tell us why the below code compiles and works?
Probably because it's well-formed...
Is it
legal in the constructor of class B?


Yes, why do you ask? What throws you off?


Couple of reasons:
1- I knew using a constructor initializer list was the best option.
Eckel's book sounded as if it was the ONLY option. My mistake.


No big deal. Happy to help.
2- Still confused about what happens to the initial storage allocated
to the member ma. When ma is first declared within the class
definition for B, is memory allocated to ma. If yes, what happens to
this memory after "ma = a(xx);" ?


Memory stays where it is. You have a default c-tor in 'a' which leaves
the 'x' member uninitialised. Just before this statement, 'ma.x' has
indeterminate value (you can examine it in the debugger). Here, two
things happen: a temporary is constructed with 'xx' as the constructor
argument, and the [compiler-generated] assignment operator is called
which invokes the assignment semantics on all members, which for you
means 'ma.x = sometemporaryobject.x'

[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #5

rona wrote:
Hi everyone,

Could someone tell us why the below code compiles and works? Is it
legal in the constructor of class B?

Cheers

Ronny

---------------------------------

#include <iostream>

using namespace std;

class a {
int x;
public:
void show() { cout<<x<<endl; }
a(int xx) : x(xx) {}
a() {}
};

class B {
a ma;
public:
B(int xx);
void disp() { ma.show(); }
};

B::B(int xx) {
ma = a(xx);
}
int main() {
B b(3);
b.disp();

return 0;
}


Use
B::B(int xx): ma(xx) {
}

instead.
you are assigning a temporary object otherwise.

Jun 14 '06 #6
rona wrote:
...
1- I knew using a constructor initializer list was the best option.
Eckel's book sounded as if it was the ONLY option. My mistake.
It would be the only option if class 'a' had no default constructor. But
it does have one.

If you remove the default constructor declaration from 'a' (the 'a() {}'
line), the code will no longer compile and a constructor initializer
list will become the only option of initializing 'ma' from 'B'.
2- Still confused about what happens to the initial storage allocated
to the member ma. When ma is first declared within the class definition
for B, is memory allocated to ma.
Yes, it is allocated. Initialization of that 'b' object begins with the
implicit initialization of 'ma' through a call to the default
constructor of class 'a' (mentioned above). The default constructor of
'a' does nothing, i.e. no physical initialization takes place ('ma'
still contains garbage after it), but conceptually 'ma' is "initialized".
If yes, what happens to this memory after "ma = a(xx);" ?


A temporary object of type 'a' is created and initialized with the
'a::a(int)' constructor. Then this temporary object is copied into 'ma'
using the copy assignment operator 'a& a::operator=(const a&)'. Since
you didn't declare this operator explicitly, the compiler will declare
and define it for you implicitly.

--
Best regards,
Andrey Tarasevich
Jun 15 '06 #7

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

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
19
by: JustSomeGuy | last post by:
I have a class that has a static member variable. string x; x should never change during use and should be intialized to "abcd". How does one do this?
8
by: Jef Driesen | last post by:
Hello, Suppose I have a class that looks like this: class point { protected: unsigned int m_x, m_y; // OR unsigned int m_data; public: point(); point(unsigned int x, unsigned int y);
3
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and there is something that I want to ask you about. It says "since the constructor does not return any value, the only way to handle errors that occur...
4
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve 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...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
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...
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
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,...
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
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.