Pointers and copying and deleting - Programmer

This is a discussion on Pointers and copying and deleting - Programmer ; All, I have been programming C++ MFC style for a while now however I still find pointers quite a daunting topic which I why I need your help. I have the following code which declares a pointer to a new ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Pointers and copying and deleting

  1. Pointers and copying and deleting

    All,

    I have been programming C++ MFC style for a while now however I still find
    pointers quite a daunting topic which I why I need your help. I have the
    following code which declares a pointer to a new object - pNewOb of type
    CAcctsObj, I want to copy the data in an existing object (pAccts) into
    pNewOb
    and then delete the old pAccts object.

    The problem is that when I delete pAccts, pNewOb becomes invalid.

    Can someone please help

    Here is my small code sample:


    CAcctsObj * pAccts = new CAcctsObj;

    < hard work here>
    ....
    < hard work here>

    CAcctsObj * pNewOb = new CAcctsObj;

    pNewOb = pAccts;
    delete pAccts;

    // pNewOb is now invalid


    Thanks
    PT



  2. Re: Pointers and copying and deleting

    Paul Tomlinson wrote:

    > All,
    >
    > I have been programming C++ MFC style for a while now however I still find
    > pointers quite a daunting topic which I why I need your help. I have the
    > following code which declares a pointer to a new object - pNewOb of type
    > CAcctsObj, I want to copy the data in an existing object (pAccts) into
    > pNewOb
    > and then delete the old pAccts object.
    >
    > The problem is that when I delete pAccts, pNewOb becomes invalid.
    >
    > Can someone please help
    >
    > Here is my small code sample:
    >
    >
    > CAcctsObj * pAccts = new CAcctsObj;
    >
    > < hard work here>
    > ...
    > < hard work here>
    >
    > CAcctsObj * pNewOb = new CAcctsObj;
    >
    > pNewOb = pAccts;
    > delete pAccts;
    >
    > // pNewOb is now invalid
    >
    >
    > Thanks
    > PT
    >
    >


    Yes, it is invalid. In fact, you also have created a memory leak by
    overwriting the pointer to the second object. You have two pointers
    pointing at the exact same object. When you delete the object both
    pointers therefore become invalid.

    pNewOb = pAccts;

    This assignment does not copy the object. It copies the pointer. To
    copy the object you have to copy each member. This is usually done with
    a copy constructor.

    --
    Scott McPhillips [VC++ MVP]


  3. Re: Pointers and copying and deleting

    In article ,
    "Scott McPhillips [MVP]" wrote:

    > Paul Tomlinson wrote:
    >
    > > All,
    > >
    > > I have been programming C++ MFC style for a while now however I still find
    > > pointers quite a daunting topic which I why I need your help. I have the
    > > following code which declares a pointer to a new object - pNewOb of type
    > > CAcctsObj, I want to copy the data in an existing object (pAccts) into
    > > pNewOb
    > > and then delete the old pAccts object.
    > >
    > > The problem is that when I delete pAccts, pNewOb becomes invalid.
    > >
    > > Can someone please help
    > >
    > > Here is my small code sample:
    > >
    > >
    > > CAcctsObj * pAccts = new CAcctsObj;
    > >
    > > < hard work here>
    > > ...
    > > < hard work here>
    > >
    > > CAcctsObj * pNewOb = new CAcctsObj;
    > >
    > > pNewOb = pAccts;
    > > delete pAccts;
    > >
    > > // pNewOb is now invalid
    > >
    > >
    > > Thanks
    > > PT
    > >
    > >

    >
    > Yes, it is invalid. In fact, you also have created a memory leak by
    > overwriting the pointer to the second object. You have two pointers
    > pointing at the exact same object. When you delete the object both
    > pointers therefore become invalid.
    >
    > pNewOb = pAccts;
    >
    > This assignment does not copy the object. It copies the pointer. To
    > copy the object you have to copy each member. This is usually done with
    > a copy constructor.


    If you've defined an assignment operator in CAcctsObj

    class CAcctsObj : public CObject {
    ....
    CAcctsObj& operator = (const CAcctsObj& inObj);
    ....
    };


    CAcctsObj& CAcctsObj:perator = (const CAcctsObj& inObj){
    CObject:perator = (inObj);
    .... // explicitly assign to the fields here
    }

    then, you could say:

    *pNewOb = *pAccts;
    delete pAccts;

    That is, dereference the pointers to get the objects, then assign the
    objects.

    Note: whenever you had to define a virtual destructor, you should also
    have defined an assignment operator, as above, and also a copy
    constructor:
    class CAcctsObj : public CObject {
    ....
    CAcctsObj(const CAcctsObj& inObj)
    ....
    };

    The copy constructor is automaticly applied when you call a fuction that
    takes a CAcctsObj (not a CAcctsObj& or CAcctsObj*) as an argument.


    Often the copy constructor and the assignment operator are DECLARED
    private but never defined, so the compiler will complain if anyone trys
    to use them.

+ Reply to Thread