Reference confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Naren

    Reference confusion

    Hello Grp,
    I find this a curious thing.

    Below is a code

    #include <iostream>
    using namespace std;

    class B
    {
    public:
    B(){cout << "constructe d B\n";}
    void f(){cout << "B Hello\n";}
    ~B(){cout << "Destroyed B\n";}
    };

    class A
    {
    B &obj;
    public:
    A(B &b):obj(b){c out << "constructe d A\n";}
    void f(){ cout << "A Hello\n"; obj.f();}
    ~A(){cout << "Destroyed A\n";}
    };

    class C
    {
    A& obj;
    public:

    C(A &a):obj(a){c out << "constructe d C\n";};
    ~C(){cout << "Destroyed C\n";}
    void f(){ cout << "C Hello\n";obj.f( );}
    };

    int main()
    {
    B b;
    C *c = new C(A(b));
    c->f();
    delete c;
    return 0;
    }

    The output of the above is
    constructed B
    constructed A
    constructed C
    Destroyed A
    C Hello
    A Hello
    B Hello
    Destroyed C
    Destroyed B

    The local object A gets constructed and destroyed But the object C which has
    a reference to this object still exists .This should have been a violation I
    believe.

    Could anyone explain this

    Thaanx in advance

    Rgds,
    Naren.



  • Karl Heinz Buchegger

    #2
    Re: Reference confusion



    Naren wrote:[color=blue]
    >
    > int main()
    > {
    > B b;
    > C *c = new C(A(b));[/color]

    It is here where the A object, a temporary, gets created and destroyed.
    [color=blue]
    > c->f();
    > delete c;
    > return 0;
    > }
    >
    > The output of the above is
    > constructed B
    > constructed A
    > constructed C
    > Destroyed A
    > C Hello
    > A Hello
    > B Hello
    > Destroyed C
    > Destroyed B
    >
    > The local object A gets constructed and destroyed,[/color]

    yep.
    [color=blue]
    > But the object C which has
    > a reference to this object still exists. This should have been a violation I
    > believe.[/color]

    It is undefined behaviour. Anything can happen. That includes: appears to work.
    Your programs behaviour may change when things are added to it, the moon has
    a different phase, at high water, etc...

    If you have a reference to another object, it is up to you, the programmer,
    to ensure that the object still is alive.

    It's pretty much the same as in:

    B* pB = new B;
    A MyA( *pB );
    delete pB;

    // now MyA is left with a reference to a B object, which no longer exists.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Josephine Schafer

      #3
      Re: Reference confusion


      "Naren" <narendranath.t s@in.bosch.com> wrote in message
      news:bjpcn0$a5e $1@ns2.fe.inter net.bosch.com.. .[color=blue]
      > Hello Grp,
      > I find this a curious thing.
      >
      > Below is a code
      >
      > #include <iostream>
      > using namespace std;
      >
      > class B
      > {
      > public:
      > B(){cout << "constructe d B\n";}
      > void f(){cout << "B Hello\n";}
      > ~B(){cout << "Destroyed B\n";}
      > };
      >
      > class A
      > {
      > B &obj;
      > public:
      > A(B &b):obj(b){c out << "constructe d A\n";}
      > void f(){ cout << "A Hello\n"; obj.f();}
      > ~A(){cout << "Destroyed A\n";}
      > };
      >
      > class C
      > {
      > A& obj;
      > public:
      >
      > C(A &a):obj(a){c out << "constructe d C\n";};
      > ~C(){cout << "Destroyed C\n";}
      > void f(){ cout << "C Hello\n";obj.f( );}
      > };
      >
      > int main()
      > {
      > B b;
      > C *c = new C(A(b));
      > c->f();
      > delete c;
      > return 0;
      > }
      >
      > The output of the above is
      > constructed B
      > constructed A
      > constructed C
      > Destroyed A
      > C Hello
      > A Hello
      > B Hello
      > Destroyed C
      > Destroyed B
      >
      > The local object A gets constructed and destroyed But the object C which[/color]
      has[color=blue]
      > a reference to this object still exists .This should have been a violation[/color]
      I[color=blue]
      > believe.
      >
      > Could anyone explain this
      >[/color]

      The code does not compile on VC 7.0 and Comeau 4.3.3 (strict mode).
      [color=blue]
      > C *c = new C(A(b));[/color]
      Taking reference to a temporary object is not correct.
      g++ 3.2 accepts it in normal mode :-(
      IMHO, the code is calling for undefined behavior.

      HTH,
      J.Schafer






      Comment

      Working...