473,472 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference confusion

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):obj(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):obj(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.

Jul 19 '05 #1
2 1199


Naren wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

"Naren" <na*************@in.bosch.com> wrote in message
news:bj**********@ns2.fe.internet.bosch.com...
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):obj(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):obj(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


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
2
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with my solution and hoping that you all can suggest a resolution. I have a common library that (A) that three other projects make reference of (B,C,D). ...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
0
by: Justin | last post by:
When creating an executible received this error during the linking... g++32 -L/usr/lib/x86_64-redhat-linux3E/lib64 -o confusion cdb.o conindb.o confusdb.o confusion.o ../../arc/lib/arc.a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.