thomas
10/1/2008 8:31:00 AM
On Oct 1, 4:19 pm, thomas <FreshTho...@gmail.com> wrote:
> Hi,
>
> ------------code--------------------------------------
> #include<iostream>
> using namespace std;
>
> class B{
> int y;
> public:
> B(int b=3):y(b){}
> int getInt(){
> return y;
> }
>
> };
>
> class A{
> int x;
> public:
> A(const int &x_){ //Line 1
> x=x_;
> }
> A(B *b){
> A(b->getInt()); //Line 2
> }
> int getInt(){
> return x;
> }
>
> };
>
> int main(){
> B *b = new B;
> A *a = new A(b);
> cout<<a->getInt()<<endl;}
>
> ---------------------------------------------------------
> I expected that in line 2, "b->getInt()" is 3, and by calling
> construction function of Line 1, the printed result should be 2;
>
> But it's 0, can anyone tell me the reason? Thanks in advance.
ok, got it, cannot call constructor in a constructor.