reference member variable question

B

Bart Simpson

If a class has a member variable that is a reference. What happens to
teh class that is being referenced, when the containing class is destroyed?

e.g.

Class A{ };

Class B
{
B(const A& a):m_a(a){}
A& m_a ;
};

int main()
{
A a;
B * b = new B(a);
delete b ; // is a deleted also at this point ?
};
 
S

Salt_Peter

If a class has a member variable that is a reference. What happens to
teh class that is being referenced, when the containing class is destroyed?

e.g.

Class A{ };

class A { };

class B
{
public:

B(const A& a):m_a(a){}
A& m_a ;

};

int main()
{
A a;
B * b = new B(a);
delete b ; // is a deleted also at this point ?

};


No, the instance 'a' dies at the end of the scope its in, namely - the
closing brace of int main() in this case. In other words: an instance
of class B does not 'own' the instance of type A. If you require 'a'
to die with the deallocation of *b, you'ld probably want a member of
type A in class B.

class B
{
A a;
public:
B() : a() {}
B( const A& r_a ) : a( r_a ) {}
};

And nothing stops you from declaring and defining a member reference
to private member 'a'.
 
B

Bart Simpson

Salt_Peter said:
class A { };




class B





No, the instance 'a' dies at the end of the scope its in, namely - the
closing brace of int main() in this case. In other words: an instance
of class B does not 'own' the instance of type A. If you require 'a'
to die with the deallocation of *b, you'ld probably want a member of
type A in class B.

class B
{
A a;
public:
B() : a() {}
B( const A& r_a ) : a( r_a ) {}
};

And nothing stops you from declaring and defining a member reference
to private member 'a'.

Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is
destroyed - unless some kind of "reference counting" is employed "under
the hood" ?. BTW this is the desired behaviour - I just dont understand
how or why it works though ... and am seeking more of an insight to
explain this (maybe someone has a copy of the language reference)?
 
J

jg

Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is

'delete b' only deletes the object b, just as 'new B(a)' only creates
b.

The object a is created by 'A a', not inside B. (For Java, 'A a' does
not
create object a, maybe you are confused with Java.)

destroyed - unless some kind of "reference counting" is employed "under
the hood" ?. BTW this is the desired behaviour - I just dont understand

No reference counting here. C++ may have garbage collection, but not
like Java's

JG
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is
destroyed - unless some kind of "reference counting" is employed "under
the hood" ?

No, a reference is *not* the object itself, the object and the
reference can have totally different scope (as in your example). A
reference refer to an object, but just like a reference in a book is
not what was referred to a reference in C++ is not the object referred
to. However a reference behaves much like the object itself in that
all operations on the reference will be performed on the object, kind
of like a proxy object.
 
S

Salt_Peter

Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is
destroyed - unless some kind of "reference counting" is employed "under
the hood" ?. BTW this is the desired behaviour - I just dont understand
how or why it works though ... and am seeking more of an insight to
explain this (maybe someone has a copy of the language reference)?

A reference is a permanent alias, its not the target object itself.
An object's lifetime is governed by the scope its in. Regardless of
whether a pointer of reference is targetting it.
Many will argue that a reference is an independant object altogether,
except that it happens to share its target's interface (public member
functions), returns its taget's address but it lives in its own
world / dimension / scope.

It doesn't make sense to have one object and 20 references to it and
then claim that you have 21 objects.
If your name is Bartholomy and you have a reference/alias of Bart -
thats still the same person.
If i chose to remove Bart from my list of friends - that doesn't
delete you. I'ld have to delete Bartholomy to do that.
If i slap Bart accross the face, only one person got slapped -
Bartholomy.
 
J

James Kanze

Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is
destroyed

The reference "is the object", except when it's not:). More
correctly, the name of the reference refers to the object. (the
reference is another name for the object). It is not, in
itself, the object, however, but is a reference. When you
destruct B, the reference (and not the object it refers to) is
destructed; destructing a reference is a no-op.
 
S

siddhu

Class A{ };


Class B
{
B(const A& a):m_a(a){}
A& m_a ;



};
Few errors in your code.

I) Class keyword should be replaced by class.

2)B(const A& a):m_a(a) {} should be B( A& a):m_a(a)
m_a is not const reference so can not refer to a const object (const
A& a).
Or you can do
Class B
{
B(const A& a):m_a(a){}
const A& m_a ;

};

It all depends on your implementation.
 
J

Jim Langston

Bart Simpson said:
Class B contains a reference to class A. since a reference IS the object
itself, I dont understand how come A is not destroyed when B is
destroyed - unless some kind of "reference counting" is employed "under
the hood" ?. BTW this is the desired behaviour - I just dont understand
how or why it works though ... and am seeking more of an insight to
explain this (maybe someone has a copy of the language reference)?

I like to call a reference a "pointer on steroids". If it was a pointer
instead,
A*m_a ;
you could see how it's possible for the pointer to go out of scope and leave
what it was pointing to untouched. A pointer going out of scope does not
call the destructor on what the pointer was pointing to, only the pointer
itself.

Same with a reference. A reference points to another variable somewhere.
When the reference goes out of scope, it gets deleted, not what it was
pointing to.

If, in this context, you think of a reference as simply a pointer you don't
have to derefernce (dont' have to use -> but can use . Don't have to use *
but the reference naem itself, etc...) then it should become clearer, as
long as you understand pointers.
 

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,297
Messages
2,571,529
Members
48,242
Latest member
BarbMott55

Latest Threads

Top