Bug? Parametrized constructor invokes default constructor

Discussion in 'C++' started by mario.rossi@REMOVETHISTOMAILMENOT.com, Oct 12, 2006.

  1. Guest

    Hi all,
    I am trying to invoke the default constructor from another, parametrized,
    constructor, but the default constructor doesn't get invoked at all, I saw.
    Is this correct ISO C++ behaviour or my compiler has a ++bug (overflowing)?

    struct Test {
    int x;
    int y;
    //
    Test() {
    x=1;
    y=2;
    }
    Test(int a) {
    Test();
    x+=a;
    }
    };

    now, if I do:

    Test Instance;

    I will get 1 in x and 2 in y, as I expect.

    But if I do:

    Test Instance(3);

    I will get garbage both in x and y, actually in x I will get garbage+3
    (so to speak) and in y I will get garbage (the value that the stack
    contained from previous use).

    Ain't it possible to invoke the default constructor from another
    constructor, or my compiler (VC++2005) has just another bug (TM)?

    Thanks,
    Mario
     
    , Oct 12, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi all,
    > I am trying to invoke the default constructor from another, parametrized,
    > constructor, but the default constructor doesn't get invoked at all, I saw.
    > Is this correct ISO C++ behaviour or my compiler has a ++bug (overflowing)?
    >
    > struct Test {
    > int x;
    > int y;
    > //
    > Test() {
    > x=1;
    > y=2;
    > }
    > Test(int a) {
    > Test();
    > x+=a;
    > }
    > };
    >
    > now, if I do:
    >
    > Test Instance;
    >
    > I will get 1 in x and 2 in y, as I expect.
    >
    > But if I do:
    >
    > Test Instance(3);
    >
    > I will get garbage both in x and y, actually in x I will get garbage+3
    > (so to speak) and in y I will get garbage (the value that the stack
    > contained from previous use).
    >
    > Ain't it possible to invoke the default constructor from another
    > constructor, or my compiler (VC++2005) has just another bug (TM)?


    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
     
    , Oct 12, 2006
    #2
    1. Advertising

  3. LR Guest

    wrote:

    > wrote:
    >
    >>Hi all,
    >>I am trying to invoke the default constructor from another, parametrized,
    >>constructor, but the default constructor doesn't get invoked at all, I saw.
    >>Is this correct ISO C++ behaviour or my compiler has a ++bug (overflowing)?
    >>
    >>struct Test {
    >> int x;
    >> int y;
    >> //
    >> Test() {
    >> x=1;
    >> y=2;
    >> }
    >> Test(int a) {
    >> Test();
    >> x+=a;
    >> }
    >>};
    >>
    >>now, if I do:
    >>
    >>Test Instance;
    >>
    >>I will get 1 in x and 2 in y, as I expect.
    >>
    >>But if I do:
    >>
    >>Test Instance(3);
    >>
    >>I will get garbage both in x and y, actually in x I will get garbage+3
    >>(so to speak) and in y I will get garbage (the value that the stack
    >>contained from previous use).
    >>
    >>Ain't it possible to invoke the default constructor from another
    >>constructor, or my compiler (VC++2005) has just another bug (TM)?

    >
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3
    >


    Excuse me but wasn't there a thread about this just a week or two ago.
    I asked then, but got no reply, if it would be possible to do this:

    (I didn't try to compile this)
    struct Test {
    int x, y;
    void swap(Test &t) {
    std::swap(t.x,x);
    std::swap(t.y,y);
    }
    Test() : x(1), y(2) {}
    Test(int a) {
    Test temp;
    swap(temp);
    x += a;
    }
    };

    Is this valid?

    LR
     
    LR, Oct 12, 2006
    #3
  4. mlimber Guest

    LR wrote:
    > Excuse me but wasn't there a thread about this just a week or two ago.
    > I asked then, but got no reply, if it would be possible to do this:
    >
    > (I didn't try to compile this)
    > struct Test {
    > int x, y;
    > void swap(Test &t) {
    > std::swap(t.x,x);
    > std::swap(t.y,y);
    > }
    > Test() : x(1), y(2) {}
    > Test(int a) {
    > Test temp;
    > swap(temp);
    > x += a;
    > }
    > };
    >
    > Is this valid?


    Yes, you create an extra object that is default initialized and then
    swap its member values for the current object's. There are better ways
    to accomplish the same thing than writing obscure code. See, e.g., at
    the end of this FAQ:

    http://parashift.com/c -faq-lite/ctors.html#faq-10.3

    Cheers! --M
     
    mlimber, Oct 12, 2006
    #4
  5. Salt_Peter Guest

    mlimber wrote:
    > LR wrote:
    > > Excuse me but wasn't there a thread about this just a week or two ago.
    > > I asked then, but got no reply, if it would be possible to do this:
    > >
    > > (I didn't try to compile this)
    > > struct Test {
    > > int x, y;
    > > void swap(Test &t) {
    > > std::swap(t.x,x);
    > > std::swap(t.y,y);
    > > }
    > > Test() : x(1), y(2) {}
    > > Test(int a) {
    > > Test temp;
    > > swap(temp);
    > > x += a;
    > > }
    > > };
    > >
    > > Is this valid?

    >
    > Yes, you create an extra object that is default initialized and then
    > swap its member values for the current object's. There are better ways
    > to accomplish the same thing than writing obscure code. See, e.g., at
    > the end of this FAQ:
    >
    > http://parashift.com/c -faq-lite/ctors.html#faq-10.3
    >
    > Cheers! --M


    In this case, even that is overkill:

    struct Test
    {
    int x;
    int y;
    public:
    Test() : x(1), y(2) { }
    Test::Test(int a) : x(a+1), y(2) { }
    };

    would do.
     
    Salt_Peter, Oct 12, 2006
    #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. Don
    Replies:
    0
    Views:
    4,351
  2. Mich
    Replies:
    5
    Views:
    539
  3. Oliver Bleckmann
    Replies:
    4
    Views:
    305
    Gavin Deane
    Dec 1, 2006
  4. Yogi
    Replies:
    3
    Views:
    1,656
    Roedy Green
    Nov 3, 2007
  5. asit
    Replies:
    6
    Views:
    847
    Alf P. Steinbach /Usenet
    May 4, 2011
Loading...

Share This Page