creating a temporary object using dynamically created object

Discussion in 'C++' started by Jess, May 17, 2007.

  1. Jess

    Jess Guest

    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():x(10){}
    };

    class B{
    public:
    A* p;
    B():p(0){}
    B(A* a):p(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
    Jess, May 17, 2007
    #1
    1. Advertising

  2. 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():x(10){}
    > };
    >
    > class B{
    > public:
    > A* p;
    > B():p(0){}
    > B(A* a):p(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
    Victor Bazarov, May 17, 2007
    #2
    1. Advertising

  3. Jess

    siddhu Guest

    On May 17, 8:40 am, Jess <> 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():x(10){}
    >
    > };
    >
    > class B{
    > public:
    > A* p;
    > B():p(0){}
    > B(A* a):p(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
    siddhu, May 17, 2007
    #3
  4. Jess

    siddhu Guest

    On May 17, 9:33 am, "Victor Bazarov" <> 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():x(10){}
    > > };

    >
    > > class B{
    > > public:
    > > A* p;
    > > B():p(0){}
    > > B(A* a):p(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 -
    siddhu, May 17, 2007
    #4
  5. siddhu wrote:
    > On May 17, 9:33 am, "Victor Bazarov" <> 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():x(10){}
    >>> };

    >>
    >>> class B{
    >>> public:
    >>> A* p;
    >>> B():p(0){}
    >>> B(A* a):p(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
    Victor Bazarov, May 17, 2007
    #5
  6. Jess

    siddhu Guest

    On May 17, 2:29 pm, "Victor Bazarov" <> wrote:
    > siddhu wrote:
    > > On May 17, 9:33 am, "Victor Bazarov" <> 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():x(10){}
    > >>> };

    >
    > >>> class B{
    > >>> public:
    > >>> A* p;
    > >>> B():p(0){}
    > >>> B(A* a):p(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 -
    siddhu, May 17, 2007
    #6
  7. siddhu wrote:
    > On May 17, 2:29 pm, "Victor Bazarov" <> wrote:
    >> siddhu wrote:
    >>> On May 17, 9:33 am, "Victor Bazarov" <>
    >>> 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():x(10){}
    >>>>> };

    >>
    >>>>> class B{
    >>>>> public:
    >>>>> A* p;
    >>>>> B():p(0){}
    >>>>> B(A* a):p(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
    Victor Bazarov, May 17, 2007
    #7
  8. Jess

    Jess Guest

    On May 18, 4:29 am, "Victor Bazarov" <> wrote:
    > siddhu wrote:
    > > On May 17, 9:33 am, "Victor Bazarov" <> 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():x(10){}
    > >>> };

    >
    > >>> class B{
    > >>> public:
    > >>> A* p;
    > >>> B():p(0){}
    > >>> B(A* a):p(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
    Jess, May 18, 2007
    #8
  9. Jess

    Nindi Guest

    On May 17, 1:40 pm, Jess <> 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():x(10){}
    >
    > };
    >
    > class B{
    > public:
    > A* p;
    > B():p(0){}
    > B(A* a):p(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
    Nindi, May 18, 2007
    #9
  10. Jess wrote:
    > On May 18, 4:29 am, "Victor Bazarov" <> wrote:
    >> siddhu wrote:
    >>> On May 17, 9:33 am, "Victor Bazarov" <>
    >>> 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():x(10){}
    >>>>> };

    >>
    >>>>> class B{
    >>>>> public:
    >>>>> A* p;
    >>>>> B():p(0){}
    >>>>> B(A* a):p(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
    Victor Bazarov, May 18, 2007
    #10
  11. Jess

    James Kanze Guest

    On May 17, 8:29 pm, "Victor Bazarov" <> wrote:
    > siddhu wrote:
    > > On May 17, 9:33 am, "Victor Bazarov" <> wrote:


    [...]
    > >> (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.


    Are you suggesting that assignment modify the value being
    assigned from? There are cases where this might be justified,
    but they are rare and far between. You're basically reinventing
    auto_ptr, and we all know the design of auto_ptr is one of the
    most complex issues in C++. A class which does this naively, as
    you suggest, does not meet the Assignable requirements of the
    standard containers, but it fails to meet them in a way that the
    compiler will typically not detect (but that will cause subtle,
    strange behavior down the road).

    The general problem that Jess is that of object lifetime. It's
    a problem which must be addressed at the design level,
    regardless of the language involved. Only once you have decided
    at the design level what the lifetime should be can you start
    addressing the issue at the programming level.

    --
    James Kanze (GABI Software) email:
    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
    James Kanze, May 18, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page