understanding constructors and initializers

S

Scott

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
 
A

AnalogFile

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 ) {
...
}
 
S

Scott

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

Hi,

Thanks for the quick reply!
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 ); ).

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
 
D

David Lindauer

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

Hi,

Thanks for the quick reply!


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
 
S

Scott

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
 
G

Gavin Deane

David said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top