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

understanding constructors and initializers

Hello,

I am writing a C++ class, as follows:

class A
{
...
private:
int i;
B b;
}

where B is another class:

class B
{
...
private:
int j;
}

Now, my constructor for class A is:

A::A(int x) : i( x ) {
...
}

And the constructor for B is:

B::B(int y) : j( y ) {
...
}

So, my question is - how do I initialize b with variable i from within A's
constructor? I hope this makes sense, and thanks for any assistance.

Best,
Scott
Mar 5 '06 #1
5 1483
Scott wrote:
....
So, my question is - how do I initialize b with variable i from within A's
constructor? I hope this makes sense, and thanks for any assistance.


If I understand what you mean, just do it exactly the same way you
initialized the other member:

A::A(int x) : i( x ), b( x ) {
...
}
Mar 5 '06 #2
On Sun, 05 Mar 2006 02:27:40 +0100, AnalogFile wrote:

Hi,

Thanks for the quick reply!
Scott wrote:
...
So, my question is - how do I initialize b with variable i from within
A's constructor? I hope this makes sense, and thanks for any
assistance.


If I understand what you mean, just do it exactly the same way you
initialized the other member:

A::A(int x) : i( x ), b( x ) {
...
}


Wouldn't this require b and x to be of the same type (i.e. int)? Making
it equivalent to writing b = x? But what I need is to construct an object
of type B, whose constructor needs an input of type int (i.e. B b( x ); ).

Another way of thinking about it - if I declare the following in class A:

class A
{
...
private:
B b;
}

And then in some method of A do this:

void
A::initB()
{
b = B( x );
}

Is that valid? If B only has a constructor of the form B::B( int y ),
then how does that ever get called from class A? Clearly I'm very
confused...

Maybe I should give a bit more detail...

In my main function, I define and declare b all at the same time, like so:

B b ( x );

But now I'm rewriting what occurs in main into it's own class (A). So b
becomes a private variable in class A, of type B (defined as above). b,
then, has already been declared, but I must still define it to call the
constructor, right?

I need a drink...

Best,
Scott
Mar 5 '06 #3


Scott wrote:
On Sun, 05 Mar 2006 02:27:40 +0100, AnalogFile wrote:

Hi,

Thanks for the quick reply!
Scott wrote:
...
So, my question is - how do I initialize b with variable i from within
A's constructor? I hope this makes sense, and thanks for any
assistance.


If I understand what you mean, just do it exactly the same way you
initialized the other member:

A::A(int x) : i( x ), b( x ) {
...
}


Wouldn't this require b and x to be of the same type (i.e. int)? Making
it equivalent to writing b = x? But what I need is to construct an object
of type B, whose constructor needs an input of type int (i.e. B b( x ); ).


the above construct actually looks for a matching constructor. for i(x) it
becomes an assignment statement i = x because i is a POD, but for b(x) where b
is a structured type it looks for a matching constructor, e.g. x is an int so
in this case it looks for a constructor B(int). If it finds this constructor
it will use it to create the embedded data member, otherwise you get an error.

David

Mar 5 '06 #4
On Sat, 04 Mar 2006 23:21:39 -0500, David Lindauer wrote:
the above construct actually looks for a matching constructor. for i(x)
it becomes an assignment statement i = x because i is a POD, but for
b(x) where b is a structured type it looks for a matching constructor,
e.g. x is an int so in this case it looks for a constructor B(int). If
it finds this constructor it will use it to create the embedded data
member, otherwise you get an error.


I see! You learn something new every day. Thanks for taking the time to
respond - I'll get to work implementing this.

Cheers,
Scott
Mar 5 '06 #5

David Lindauer wrote:
Scott wrote:
Scott wrote:
If I understand what you mean, just do it exactly the same way you
initialized the other member:

A::A(int x) : i( x ), b( x ) {
...
}


Wouldn't this require b and x to be of the same type (i.e. int)? Making
it equivalent to writing b = x? But what I need is to construct an object
of type B, whose constructor needs an input of type int (i.e. B b( x ); ).


the above construct actually looks for a matching constructor. for i(x) it
becomes an assignment statement i = x because i is a POD,


<nit, which doesn't affect the answer to the OP's question>
There's no assignment. i is initialised with the value of x. Equivalent
to

int i(x);
or
int i = x;

which initialise, but not

int i;
i = x;

which is assignment.

Gavin Deane

Mar 5 '06 #6

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

Similar topics

2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
8
by: Pent | last post by:
Hi All, Why is this code valid? How can the static ctor be used? It doesn't act as class ctor at all. struct A { static A() { } }
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
1
by: techiepundit | last post by:
I have a class: class ServerThreadManager(threading.Thread): def __init__(self): threading.Thread.__init__(self) # and a bunch of constructor statements def run(self): self.ReqHandlingLoop()
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
8
by: Richard | last post by:
If class C inherits from class B, which inherits from class A, is the order of the initializer list in the following constructor insignificant (C only inherits from A because it inherits from B)?:...
6
by: DeveloperX | last post by:
In an attempt to solve a problem further down I suggested the problem might be caused by a missing constructor. Now, this led me to the conclusion that I don't fully understand constructors, or...
13
by: =?Utf-8?B?QW5kcmVhcw==?= | last post by:
Hi, I would like to get some thoughts on Overloaded constructors vs. Object initializations. Assuming that the class supports a default constructor, is there any reason to include overloaded...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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.