Reference confusion

N

Naren

Hello Grp,
I find this a curious thing.

Below is a code

#include <iostream>
using namespace std;

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

class A
{
B &obj;
public:
A(B &b):eek:bj(b){cout << "constructed A\n";}
void f(){ cout << "A Hello\n"; obj.f();}
~A(){cout << "Destroyed A\n";}
};

class C
{
A& obj;
public:

C(A &a):eek:bj(a){cout << "constructed 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.
 
K

Karl Heinz Buchegger

Naren said:
int main()
{
B b;
C *c = new C(A(b));

It is here where the A object, a temporary, gets created and destroyed.
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,
yep.

But the object C which has
a reference to this object still exists. This should have been a violation I
believe.

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

Josephine Schafer

Naren said:
Hello Grp,
I find this a curious thing.

Below is a code

#include <iostream>
using namespace std;

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

class A
{
B &obj;
public:
A(B &b):eek:bj(b){cout << "constructed A\n";}
void f(){ cout << "A Hello\n"; obj.f();}
~A(){cout << "Destroyed A\n";}
};

class C
{
A& obj;
public:

C(A &a):eek:bj(a){cout << "constructed 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

The code does not compile on VC 7.0 and Comeau 4.3.3 (strict mode).
C *c = new C(A(b));
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top