initialization failure

Discussion in 'C++' started by thomas, Oct 1, 2008.

  1. thomas

    thomas Guest

    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.
    thomas, Oct 1, 2008
    #1
    1. Advertising

  2. thomas

    thomas Guest

    On Oct 1, 4:19 pm, thomas <> 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.
    thomas, Oct 1, 2008
    #2
    1. Advertising

  3. thomas

    Guest

    On Oct 1, 4:19 pm, thomas <> wrote:
    >         A(B *b){
    >                 A(b->getInt());        //Line  2
    >         }


    > ---------------------------------------------------------
    > 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.


    This is because in Line 2, x member of the current object is not being
    initialized, instead a new A object is created and it's x is
    initialized. You need to simply initialize x of the current object.

    A(B *b)
    {
    // probably have a check to see that b is not NULL before
    accessing b ptr
    x = b->getInt();
    }



    Thanks and regards
    Sonison James
    , Oct 1, 2008
    #3

  4. > ok, got it, cannot call constructor in a constructor.


    sure you can, in your case, as someone already mention, you're
    creating a new object.

    Here is how you can call another ctor

    class A{
    A(){}
    A():this(){

    }

    };
    puzzlecracker, Oct 1, 2008
    #4
  5. On Oct 1, 11:03 am, Obnoxious User <O...@127.0.0.1> wrote:
    > On Wed, 01 Oct 2008 06:07:10 -0700, puzzlecracker wrote:
    > >> ok, got it, cannot call constructor in a constructor.

    >
    > > sure you can, in your case, as someone already mention, you're creating
    > > a new object.

    >
    > > Here is how you can call another ctor

    >
    > > class A{
    > > A(){}
    > > A():this(){

    >
    > > }

    >
    > > };

    >
    > No you can't.
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    >
    > --
    > OU
    > Remember 18th of June 2008, Democracy died that afternoon.http://frapedia.se/wiki/Information_in_English


    Opps, I was referring to calling the base class ctor. Sorry!
    puzzlecracker, Oct 2, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    564
    Ilias Lazaridis
    Feb 1, 2005
  2. JKop
    Replies:
    10
    Views:
    927
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    432
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  5. Replies:
    0
    Views:
    457
Loading...

Share This Page