By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,436 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

initializing a type from another

P: n/a
Hi

I am not sure how to initialize this.

B::B(int, double): /*...*/ {}

class A(
B b;
public:
A();
};
A::A(int x, double y):b(x,y){} //<<< is this correct?
thanks
Dec 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gary Wessle wrote:
B::B(int, double): /*...*/ {}

class A(
B b;
public:
A();
};
A::A(int x, double y):b(x,y){} //<<< is this correct?
You've got the idea - but the declaration for A's constructor should be
A(int, double) rather than A().

Dec 20 '06 #2

P: n/a
Here is the sample code

class B
{
public:
B(int x, float y)
{
cout<<" in the B"<<endl;
}

};
class A
{
public:
B b;
A(int x ,float y);
~A(){cout << " in the destructor"<<endl;}
};
A::A(int x, float y):b(x,y)
{
cout<< "in the A";
}
Gary Wessle wrote:
Hi

I am not sure how to initialize this.

B::B(int, double): /*...*/ {}

class A(
B b;
public:
A();
};
A::A(int x, double y):b(x,y){} //<<< is this correct?
thanks
Dec 20 '06 #3

P: n/a

Gary Wessle wrote:
Hi

I am not sure how to initialize this.
add another ctor in B, a def ctor...
B::B() : x(0), y(0.0) { } // adjust to match member names
B::B(int, double): /*...*/ {}
B::B(int n, double d) : x(n), y(d) { } // adjust member names
>
class A(
B b;
public:
A();
add your parametized ctor declaration, leave the def A() ctor above:
A(int x, double y);
or
A(int, double);
};
adding impl for def ctor...
A::A() { } // b's def ctor invoked
A::A(int x, double y):b(x,y){} //<<< is this correct?
Yes, except for the fact that it wasn't declared in the class.
>

thanks
Dec 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.