Username: Password:
Not registered yet? Click Here
Title/Subject only
Title -and- text
Software Forums: Display Message

Author: Hartwell Pablo
E-mail: [email protected]
Date: 2/25/2003 4:37:28 PM
Subject: overloading pointer assignment
Message: I understand that the code to overload the assignment operator performs a deep copy. However, I need to perform a shallow copy.

ClassA* pObj1 = new ClassA;
ClassA* pObj2 = new ClassA;

//Assuming both objects were initialized...
pObj2 = pObj1;

I'm trying to implement a memory management strategy where all memory is statically allocated at startup. When a pointer is needed, the overloaded new operator grabs an available memory address from a maintained stack of free pointers. Likewise, when memory should be freed, the overload delete operator puts the available memory back on the stack. This concept works fine.

Now, the problem is I have to address the mem mgmt issue of when a pointer assigment occurs. Before pObj2's address is assigned to pObj1's address, pObj2's old address must be "pushed" back onto the free-memory stack. Any thoughts on how to do this?

I've come up with this so far:
ClassA& ClassA::operator=(ClassA &rhs)
if (this != &rhs)
    //the stack maintains an array of unsigned ints
    DWORD oldAddress = (DWORD)this;

    *this = rhs; //this causes an infinite loop! I can see why, but I don't see any other way around it.
return rhs;

Besides, overloading the assignment operator may not be what I need to do. Like I've mentioned, overloading the assignment operator performs a deep copy, while I need a shallow copy.

I'd really appreciate the help.


You must be logged in to post a reply...

Back to the Folder

 C++ -- General C++ Discussion Forum
 overloading pointer assignment by Hartwell Pablo  at 2/25/2003 4:37:28 PM
© 2002 - - All Rights Reserved ...A software developer's forum