473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reference member variable question

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 ?
};
May 3 '07 #1
8 9106
On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
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
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'.
May 4 '07 #2


Salt_Peter wrote:
On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
>>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


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

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)?
May 4 '07 #3
jg
On May 3, 5:37 pm, Bart Simpson <123evergr...@terrace.comwrote:
>
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

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)?- Hide quoted text -

- Show quoted text -

May 4 '07 #4
On 4 Maj, 02:37, Bart Simpson <123evergr...@terrace.comwrote:
Salt_Peter wrote:
On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
>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
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'.

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.

--
Erik Wikström

May 4 '07 #5
On May 3, 8:37 pm, Bart Simpson <123evergr...@terrace.comwrote:
Salt_Peter wrote:
On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
>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
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'.

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.

May 4 '07 #6
On May 4, 2:37 am, Bart Simpson <123evergr...@terrace.comwrote:
Salt_Peter wrote:
On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 4 '07 #7
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.

May 4 '07 #8
"Bart Simpson" <12**********@terrace.comwrote in message
news:uq******************************@bt.com...
>

Salt_Peter wrote:
>On May 3, 7:35 pm, Bart Simpson <123evergr...@terrace.comwrote:
>>>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


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


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.
May 5 '07 #9

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

Similar topics

1
by: roni | last post by:
my target is to create objects only as data member of class. (but offcourse i can pass the objects reference to other objects) my question: how can i make sure that my object is create as data...
2
by: Cory Burkhardt | last post by:
I am not sure what the suggested practice is for returning reference types from read-only properties. Consider an example: Suppose I have a Point class that is a reference type. I use the Point...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
11
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to...
8
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
4
by: Naren | last post by:
Hi , Is it correct to return reference of a member variable. because it can be like we have already deleted the object and still holding the reference of member variable. A *a = new A; int...
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
4
by: siddhu | last post by:
If there is reference member variable in the class, why doesn't default assignment operator work? class A { int& i; public: A( int& ii):i(ii){} //A& operator=(const A& a){i = a.i;} };
26
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.