Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > creating a temporary object using dynamically created object

Reply
Thread Tools

creating a temporary object using dynamically created object

 
 
Jess
Guest
Posts: n/a
 
      05-17-2007
Hello,

If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A()(10){}
};

class B{
public:
A* p;
B()(0){}
B(A* a)(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};


int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed. I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.

Thanks,
Jess

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2007
Jess wrote:
> If I create a temporary object using a dynamically created object's
> pointer, then when the temporary object is destroyed, will the
> dynamically created object be destroyed too? My guess is that it's
> not destroyed, but I'm not sure.
>
> I have the following program:
>
> #include<iostream>
>
> using namespace std;
>
> class A{
> public:
> int x;
> A()(10){}
> };
>
> class B{
> public:
> A* p;
> B()(0){}
> B(A* a)(a){}
> B& operator=(const B& s){
> if(&s != this){
> delete p;
> p = s.p; //copy s's pointer, so they should point to the same
> object
> }
> }
> };
>
>
> int main(){
> B b;
> b = new A;
> cout << b.p->x << endl;
> return 0;
> }
>
> In the "main" function, I first created an A object using "new", then
> the A* pointer is converted to a B object, which is temporary.


Right.

> Then,
> I assign this temporary object to "b", so that b's pointer "p" should
> also point to the dynamically created A object. After this copying is
> done, the temporary B object should be destroyed.


But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.

> I tried to output
> the value of "b.p->x", and it still has the value 10. However, it may
> still mean the value is garbage, i.e. the dynamically created object A
> object may have been destroyed.


No, it's still alive and well.

Your example is a good illustration of lifetime differences and what is
known as "ownership". Essentially, nobody /owns/ the object 'A' you
create when you say 'new A'. The temporary 'B' doesn't own it, it only
has a pointer to that object. 'b' doesn't own it either, it only takes
the copy of the pointer from the temporary.

Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory leak".

If you decide that a 'B' object should take ownership of the 'A' object
passed to it [by the pointer], then you should (a) take care of deleting
the object in the 'B' destructor (which you didn't even define), and
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because you
will need to set 's.p' to 0 upon transferring the ownership of 'p' value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
siddhu
Guest
Posts: n/a
 
      05-17-2007
On May 17, 8:40 am, Jess <(E-Mail Removed)> wrote:
> Hello,
>
> If I create a temporary object using a dynamically created object's
> pointer, then when the temporary object is destroyed, will the
> dynamically created object be destroyed too? My guess is that it's
> not destroyed, but I'm not sure.
>
> I have the following program:
>
> #include<iostream>
>
> using namespace std;
>
> class A{
> public:
> int x;
> A()(10){}
>
> };
>
> class B{
> public:
> A* p;
> B()(0){}
> B(A* a)(a){}
> B& operator=(const B& s){
> if(&s != this){
> delete p;
> p = s.p; //copy s's pointer, so they should point to the same
> object
> }
> }
>
> };
>
> int main(){
> B b;
> b = new A;
> cout << b.p->x << endl;
> return 0;
>
> }
>
> In the "main" function, I first created an A object using "new", then
> the A* pointer is converted to a B object, which is temporary. Then,
> I assign this temporary object to "b", so that b's pointer "p" should
> also point to the dynamically created A object. After this copying is
> done, the temporary B object should be destroyed.

Temporary is destroyed but p is still pointing to undeleted memory. So
no harm.
In this case it will be problematic if you write destructor like this
~B(){delete p;}
I tried to output
> the value of "b.p->x", and it still has the value 10.


As it should
> However, it may
> still mean the value is garbage, i.e. the dynamically created object A
> object may have been destroyed.


NO
>
> Thanks,
> Jess



 
Reply With Quote
 
siddhu
Guest
Posts: n/a
 
      05-17-2007
On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Jess wrote:
> > If I create a temporary object using a dynamically created object's
> > pointer, then when the temporary object is destroyed, will the
> > dynamically created object be destroyed too? My guess is that it's
> > not destroyed, but I'm not sure.

>
> > I have the following program:

>
> > #include<iostream>

>
> > using namespace std;

>
> > class A{
> > public:
> > int x;
> > A()(10){}
> > };

>
> > class B{
> > public:
> > A* p;
> > B()(0){}
> > B(A* a)(a){}
> > B& operator=(const B& s){
> > if(&s != this){
> > delete p;
> > p = s.p; //copy s's pointer, so they should point to the same
> > object
> > }
> > }
> > };

>
> > int main(){
> > B b;
> > b = new A;
> > cout << b.p->x << endl;
> > return 0;
> > }

>
> > In the "main" function, I first created an A object using "new", then
> > the A* pointer is converted to a B object, which is temporary.

>
> Right.
>
> > Then,
> > I assign this temporary object to "b", so that b's pointer "p" should
> > also point to the dynamically created A object. After this copying is
> > done, the temporary B object should be destroyed.

>
> But 'b' retained that pointer, you copied it to 'b' yourself in the
> assignment operator.
>
> > I tried to output
> > the value of "b.p->x", and it still has the value 10. However, it may
> > still mean the value is garbage, i.e. the dynamically created object A
> > object may have been destroyed.

>
> No, it's still alive and well.
>
> Your example is a good illustration of lifetime differences and what is
> known as "ownership". Essentially, nobody /owns/ the object 'A' you
> create when you say 'new A'. The temporary 'B' doesn't own it, it only
> has a pointer to that object. 'b' doesn't own it either, it only takes
> the copy of the pointer from the temporary.
>
> Since nobody owns the 'A', nobody destroys it. The object lives on
> until the program finishes executing. That's a kind of "memory leak".
>
> If you decide that a 'B' object should take ownership of the 'A' object
> passed to it [by the pointer], then you should (a) take care of deleting
> the object in the 'B' destructor (which you didn't even define), and


Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.

> (b) take care of the transfer of ownership in the assignment operator
> which should most likely take a reference to non-const 'B' because you
> will need to set 's.p' to 0 upon transferring the ownership of 'p' value.


Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2007
siddhu wrote:
> On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Jess wrote:
>>> If I create a temporary object using a dynamically created object's
>>> pointer, then when the temporary object is destroyed, will the
>>> dynamically created object be destroyed too? My guess is that it's
>>> not destroyed, but I'm not sure.

>>
>>> I have the following program:

>>
>>> #include<iostream>

>>
>>> using namespace std;

>>
>>> class A{
>>> public:
>>> int x;
>>> A()(10){}
>>> };

>>
>>> class B{
>>> public:
>>> A* p;
>>> B()(0){}
>>> B(A* a)(a){}
>>> B& operator=(const B& s){
>>> if(&s != this){
>>> delete p;
>>> p = s.p; //copy s's pointer, so they should point to the same
>>> object
>>> }
>>> }
>>> };

>>
>>> int main(){
>>> B b;
>>> b = new A;
>>> cout << b.p->x << endl;
>>> return 0;
>>> }

>>
>>> In the "main" function, I first created an A object using "new",
>>> then the A* pointer is converted to a B object, which is temporary.

>>
>> Right.
>>
>>> Then,
>>> I assign this temporary object to "b", so that b's pointer "p"
>>> should also point to the dynamically created A object. After this
>>> copying is done, the temporary B object should be destroyed.

>>
>> But 'b' retained that pointer, you copied it to 'b' yourself in the
>> assignment operator.
>>
>>> I tried to output
>>> the value of "b.p->x", and it still has the value 10. However, it
>>> may still mean the value is garbage, i.e. the dynamically created
>>> object A object may have been destroyed.

>>
>> No, it's still alive and well.
>>
>> Your example is a good illustration of lifetime differences and what
>> is known as "ownership". Essentially, nobody /owns/ the object 'A'
>> you
>> create when you say 'new A'. The temporary 'B' doesn't own it, it
>> only
>> has a pointer to that object. 'b' doesn't own it either, it only
>> takes
>> the copy of the pointer from the temporary.
>>
>> Since nobody owns the 'A', nobody destroys it. The object lives on
>> until the program finishes executing. That's a kind of "memory
>> leak".
>>
>> If you decide that a 'B' object should take ownership of the 'A'
>> object passed to it [by the pointer], then you should (a) take care
>> of deleting the object in the 'B' destructor (which you didn't even
>> define), and

>
> Problem: As temporary is getting created and when it will get
> destroyed memory pointed by p will also be deleted.


Not if the ownership is correctly transferred.

>
>> (b) take care of the transfer of ownership in the assignment operator
>> which should most likely take a reference to non-const 'B' because
>> you
>> will need to set 's.p' to 0 upon transferring the ownership of 'p'
>> value.

>
> Problem:Will not compile. Temporary is getting created so it can not
> be bound to non-const reference.


That's true. But the pointer can be made 'mutable' specifically for
this situation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
siddhu
Guest
Posts: n/a
 
      05-17-2007
On May 17, 2:29 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> siddhu wrote:
> > On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >> Jess wrote:
> >>> If I create a temporary object using a dynamically created object's
> >>> pointer, then when the temporary object is destroyed, will the
> >>> dynamically created object be destroyed too? My guess is that it's
> >>> not destroyed, but I'm not sure.

>
> >>> I have the following program:

>
> >>> #include<iostream>

>
> >>> using namespace std;

>
> >>> class A{
> >>> public:
> >>> int x;
> >>> A()(10){}
> >>> };

>
> >>> class B{
> >>> public:
> >>> A* p;
> >>> B()(0){}
> >>> B(A* a)(a){}
> >>> B& operator=(const B& s){
> >>> if(&s != this){
> >>> delete p;
> >>> p = s.p; //copy s's pointer, so they should point to the same
> >>> object
> >>> }
> >>> }
> >>> };

>
> >>> int main(){
> >>> B b;
> >>> b = new A;
> >>> cout << b.p->x << endl;
> >>> return 0;
> >>> }

>
> >>> In the "main" function, I first created an A object using "new",
> >>> then the A* pointer is converted to a B object, which is temporary.

>
> >> Right.

>
> >>> Then,
> >>> I assign this temporary object to "b", so that b's pointer "p"
> >>> should also point to the dynamically created A object. After this
> >>> copying is done, the temporary B object should be destroyed.

>
> >> But 'b' retained that pointer, you copied it to 'b' yourself in the
> >> assignment operator.

>
> >>> I tried to output
> >>> the value of "b.p->x", and it still has the value 10. However, it
> >>> may still mean the value is garbage, i.e. the dynamically created
> >>> object A object may have been destroyed.

>
> >> No, it's still alive and well.

>
> >> Your example is a good illustration of lifetime differences and what
> >> is known as "ownership". Essentially, nobody /owns/ the object 'A'
> >> you
> >> create when you say 'new A'. The temporary 'B' doesn't own it, it
> >> only
> >> has a pointer to that object. 'b' doesn't own it either, it only
> >> takes
> >> the copy of the pointer from the temporary.

>
> >> Since nobody owns the 'A', nobody destroys it. The object lives on
> >> until the program finishes executing. That's a kind of "memory
> >> leak".

>
> >> If you decide that a 'B' object should take ownership of the 'A'
> >> object passed to it [by the pointer], then you should (a) take care
> >> of deleting the object in the 'B' destructor (which you didn't even
> >> define), and

>
> > Problem: As temporary is getting created and when it will get
> > destroyed memory pointed by p will also be deleted.

>
> Not if the ownership is correctly transferred.

OK. As I can guess you are talking about strict ownership. Are you
suggesting to do something like s.p=NULL; in the assignment operator?

>
>
>
> >> (b) take care of the transfer of ownership in the assignment operator
> >> which should most likely take a reference to non-const 'B' because
> >> you
> >> will need to set 's.p' to 0 upon transferring the ownership of 'p'
> >> value.

>
> > Problem:Will not compile. Temporary is getting created so it can not
> > be bound to non-const reference.

>
> That's true. But the pointer can be made 'mutable' specifically for
> this situation.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2007
siddhu wrote:
> On May 17, 2:29 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> siddhu wrote:
>>> On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)>
>>> wrote:
>>>> Jess wrote:
>>>>> If I create a temporary object using a dynamically created
>>>>> object's pointer, then when the temporary object is destroyed,
>>>>> will the dynamically created object be destroyed too? My guess
>>>>> is that it's not destroyed, but I'm not sure.

>>
>>>>> I have the following program:

>>
>>>>> #include<iostream>

>>
>>>>> using namespace std;

>>
>>>>> class A{
>>>>> public:
>>>>> int x;
>>>>> A()(10){}
>>>>> };

>>
>>>>> class B{
>>>>> public:
>>>>> A* p;
>>>>> B()(0){}
>>>>> B(A* a)(a){}
>>>>> B& operator=(const B& s){
>>>>> if(&s != this){
>>>>> delete p;
>>>>> p = s.p; //copy s's pointer, so they should point to the same
>>>>> object
>>>>> }
>>>>> }
>>>>> };

>>
>>>>> int main(){
>>>>> B b;
>>>>> b = new A;
>>>>> cout << b.p->x << endl;
>>>>> return 0;
>>>>> }

>>
>>>>> In the "main" function, I first created an A object using "new",
>>>>> then the A* pointer is converted to a B object, which is
>>>>> temporary.

>>
>>>> Right.

>>
>>>>> Then,
>>>>> I assign this temporary object to "b", so that b's pointer "p"
>>>>> should also point to the dynamically created A object. After this
>>>>> copying is done, the temporary B object should be destroyed.

>>
>>>> But 'b' retained that pointer, you copied it to 'b' yourself in the
>>>> assignment operator.

>>
>>>>> I tried to output
>>>>> the value of "b.p->x", and it still has the value 10. However, it
>>>>> may still mean the value is garbage, i.e. the dynamically created
>>>>> object A object may have been destroyed.

>>
>>>> No, it's still alive and well.

>>
>>>> Your example is a good illustration of lifetime differences and
>>>> what is known as "ownership". Essentially, nobody /owns/ the
>>>> object 'A' you
>>>> create when you say 'new A'. The temporary 'B' doesn't own it, it
>>>> only
>>>> has a pointer to that object. 'b' doesn't own it either, it only
>>>> takes
>>>> the copy of the pointer from the temporary.

>>
>>>> Since nobody owns the 'A', nobody destroys it. The object lives on
>>>> until the program finishes executing. That's a kind of "memory
>>>> leak".

>>
>>>> If you decide that a 'B' object should take ownership of the 'A'
>>>> object passed to it [by the pointer], then you should (a) take care
>>>> of deleting the object in the 'B' destructor (which you didn't even
>>>> define), and

>>
>>> Problem: As temporary is getting created and when it will get
>>> destroyed memory pointed by p will also be deleted.

>>
>> Not if the ownership is correctly transferred.

> OK. As I can guess you are talking about strict ownership. Are you
> suggesting to do something like s.p=NULL; in the assignment operator?


Absolutely.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Jess
Guest
Posts: n/a
 
      05-18-2007
On May 18, 4:29 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> siddhu wrote:
> > On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >> Jess wrote:
> >>> If I create a temporary object using a dynamically created object's
> >>> pointer, then when the temporary object is destroyed, will the
> >>> dynamically created object be destroyed too? My guess is that it's
> >>> not destroyed, but I'm not sure.

>
> >>> I have the following program:

>
> >>> #include<iostream>

>
> >>> using namespace std;

>
> >>> class A{
> >>> public:
> >>> int x;
> >>> A()(10){}
> >>> };

>
> >>> class B{
> >>> public:
> >>> A* p;
> >>> B()(0){}
> >>> B(A* a)(a){}
> >>> B& operator=(const B& s){
> >>> if(&s != this){
> >>> delete p;
> >>> p = s.p; //copy s's pointer, so they should point to the same
> >>> object
> >>> }
> >>> }
> >>> };

>
> >>> int main(){
> >>> B b;
> >>> b = new A;
> >>> cout << b.p->x << endl;
> >>> return 0;
> >>> }

>
> >>> In the "main" function, I first created an A object using "new",
> >>> then the A* pointer is converted to a B object, which is temporary.

>
> >> Right.

>
> >>> Then,
> >>> I assign this temporary object to "b", so that b's pointer "p"
> >>> should also point to the dynamically created A object. After this
> >>> copying is done, the temporary B object should be destroyed.

>
> >> But 'b' retained that pointer, you copied it to 'b' yourself in the
> >> assignment operator.

>
> >>> I tried to output
> >>> the value of "b.p->x", and it still has the value 10. However, it
> >>> may still mean the value is garbage, i.e. the dynamically created
> >>> object A object may have been destroyed.

>
> >> No, it's still alive and well.

>
> >> Your example is a good illustration of lifetime differences and what
> >> is known as "ownership". Essentially, nobody /owns/ the object 'A'
> >> you
> >> create when you say 'new A'. The temporary 'B' doesn't own it, it
> >> only
> >> has a pointer to that object. 'b' doesn't own it either, it only
> >> takes
> >> the copy of the pointer from the temporary.

>
> >> Since nobody owns the 'A', nobody destroys it. The object lives on
> >> until the program finishes executing. That's a kind of "memory
> >> leak".

>
> >> If you decide that a 'B' object should take ownership of the 'A'
> >> object passed to it [by the pointer], then you should (a) take care
> >> of deleting the object in the 'B' destructor (which you didn't even
> >> define), and

>
> > Problem: As temporary is getting created and when it will get
> > destroyed memory pointed by p will also be deleted.

>
> Not if the ownership is correctly transferred.


By transfering the ownership, do you mean creating a new A object
(with the same value as the old A object) and assign it to the new
pointer, and then delete the old object and set the temporary B
object's pointer to NULL?

>
>
>
> >> (b) take care of the transfer of ownership in the assignment operator
> >> which should most likely take a reference to non-const 'B' because
> >> you
> >> will need to set 's.p' to 0 upon transferring the ownership of 'p'
> >> value.

>
> > Problem:Will not compile. Temporary is getting created so it can not
> > be bound to non-const reference.


Do you mean the non-const "b" object?

>
> That's true. But the pointer can be made 'mutable' specifically for
> this situation.


What is a mutable pointer and how does it help here?

Thanks,
Jess

 
Reply With Quote
 
Nindi
Guest
Posts: n/a
 
      05-18-2007
On May 17, 1:40 pm, Jess <(E-Mail Removed)> wrote:
> Hello,
>
> If I create a temporary object using a dynamically created object's
> pointer, then when the temporary object is destroyed, will the
> dynamically created object be destroyed too? My guess is that it's
> not destroyed, but I'm not sure.
>
> I have the following program:
>
> #include<iostream>
>
> using namespace std;
>
> class A{
> public:
> int x;
> A()(10){}
>
> };
>
> class B{
> public:
> A* p;
> B()(0){}
> B(A* a)(a){}
> B& operator=(const B& s){
> if(&s != this){
> delete p;
> p = s.p; //copy s's pointer, so they should point to the same
> object
> }
> }
>
> };
>
> int main(){
> B b;
> b = new A;
> cout << b.p->x << endl;
> return 0;
>
> }
>
> In the "main" function, I first created an A object using "new", then
> the A* pointer is converted to a B object, which is temporary. Then,
> I assign this temporary object to "b", so that b's pointer "p" should
> also point to the dynamically created A object. After this copying is
> done, the temporary B object should be destroyed. I tried to output
> the value of "b.p->x", and it still has the value 10. However, it may
> still mean the value is garbage, i.e. the dynamically created object A
> object may have been destroyed.
>
> Thanks,
> Jess


You should definitley be using boost::share_ptr

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-18-2007
Jess wrote:
> On May 18, 4:29 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> siddhu wrote:
>>> On May 17, 9:33 am, "Victor Bazarov" <(E-Mail Removed)>
>>> wrote:
>>>> Jess wrote:
>>>>> If I create a temporary object using a dynamically created
>>>>> object's pointer, then when the temporary object is destroyed,
>>>>> will the dynamically created object be destroyed too? My guess
>>>>> is that it's not destroyed, but I'm not sure.

>>
>>>>> I have the following program:

>>
>>>>> #include<iostream>

>>
>>>>> using namespace std;

>>
>>>>> class A{
>>>>> public:
>>>>> int x;
>>>>> A()(10){}
>>>>> };

>>
>>>>> class B{
>>>>> public:
>>>>> A* p;
>>>>> B()(0){}
>>>>> B(A* a)(a){}
>>>>> B& operator=(const B& s){
>>>>> if(&s != this){
>>>>> delete p;
>>>>> p = s.p; //copy s's pointer, so they should point to the same
>>>>> object
>>>>> }
>>>>> }
>>>>> };

>>
>>>>> int main(){
>>>>> B b;
>>>>> b = new A;
>>>>> cout << b.p->x << endl;
>>>>> return 0;
>>>>> }

>>
>>>>> In the "main" function, I first created an A object using "new",
>>>>> then the A* pointer is converted to a B object, which is
>>>>> temporary.

>>
>>>> Right.

>>
>>>>> Then,
>>>>> I assign this temporary object to "b", so that b's pointer "p"
>>>>> should also point to the dynamically created A object. After this
>>>>> copying is done, the temporary B object should be destroyed.

>>
>>>> But 'b' retained that pointer, you copied it to 'b' yourself in the
>>>> assignment operator.

>>
>>>>> I tried to output
>>>>> the value of "b.p->x", and it still has the value 10. However, it
>>>>> may still mean the value is garbage, i.e. the dynamically created
>>>>> object A object may have been destroyed.

>>
>>>> No, it's still alive and well.

>>
>>>> Your example is a good illustration of lifetime differences and
>>>> what is known as "ownership". Essentially, nobody /owns/ the
>>>> object 'A' you
>>>> create when you say 'new A'. The temporary 'B' doesn't own it, it
>>>> only
>>>> has a pointer to that object. 'b' doesn't own it either, it only
>>>> takes
>>>> the copy of the pointer from the temporary.

>>
>>>> Since nobody owns the 'A', nobody destroys it. The object lives on
>>>> until the program finishes executing. That's a kind of "memory
>>>> leak".

>>
>>>> If you decide that a 'B' object should take ownership of the 'A'
>>>> object passed to it [by the pointer], then you should (a) take care
>>>> of deleting the object in the 'B' destructor (which you didn't even
>>>> define), and

>>
>>> Problem: As temporary is getting created and when it will get
>>> destroyed memory pointed by p will also be deleted.

>>
>> Not if the ownership is correctly transferred.

>
> By transfering the ownership, do you mean creating a new A object
> (with the same value as the old A object) and assign it to the new
> pointer, and then delete the old object and set the temporary B
> object's pointer to NULL?
>
>>
>>
>>
>>>> (b) take care of the transfer of ownership in the assignment
>>>> operator which should most likely take a reference to non-const
>>>> 'B' because you
>>>> will need to set 's.p' to 0 upon transferring the ownership of 'p'
>>>> value.

>>
>>> Problem:Will not compile. Temporary is getting created so it can not
>>> be bound to non-const reference.

>
> Do you mean the non-const "b" object?


A temporary is non-const. However, a reference to non-const (the
argument of 'operator=(B&)' ) cannot be bound to the temporary.

>> That's true. But the pointer can be made 'mutable' specifically for
>> this situation.

>
> What is a mutable pointer and how does it help here?


Not a mutable pointer, but a mutable member.

class B {
..
B& operator =(const B& s)
{
if (&s != this) {
delete p; // get rid of mine
p = s.p; // copy the other one's pointer
s.p = 0; // make 's' forget about its 'p'
}
}
..
mutable A* p; // mutable to be able to set it in the operator=
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Creating onClick event for dynamically (programatically) created buttons Hrvoje Vrbanc ASP .Net 1 07-10-2003 04:55 PM



Advertisments