HI I NEED TO WRITE A C++ CODE[DEFINTION & DECLARATION] FOR THE NECESSARY COPY CONSTRUCTORS & ASSIGNMENT OPERATORS TO ENSURE THAT A DEEP COPY TAKES PLACE.

class CShop
{
public:
CShop();
~CShop();
 
private:
CEmployeeContainer* m_pEmployees;
char* m_strShopName;
};
 
class CEmployeeContainer
{
public:
CEmployeeContainer();
~CEmployeeContainer();
 
private:
CBoss* m_pBoss;
list<string> m_listEmployeeNames;
};
 
class CBoss
{
public:
CBoss();
~CBoss():
private:
char* m_strName;
int m_nAge;
};

The following code

CShop shop1, shop2;
shp2 =shop1;

must copy everything from shop1 into shop2 without aliasing occuring. i need to use comments where appropriate.
plz help!!!:!:

Recommended Answers

All 4 Replies

To deep copy a C-style string you need to declare enough memory for the string new copy and then use strcpy() to copy the contents of the old string into the new string.

If you are deep copying an array that isn't a C-style string then you need to declare enough memory and copy each element of the old array into the new one one at a time.

what's the difference between deep copy and shallow copy or not so deep copy :rolleyes:

what's the difference between deep copy and shallow copy or not so deep copy :rolleyes:

As far as I understand it, when copying an object containing pointers to dynamically allocated memory, a shallow copy (which occurs by default) only copies the bare pointer members, so that the two objects both refer to the same block(s) of dynamically allocated memory.

On the other hand, take the STL <list> for example - when you create a copy, all the elements pointed by the list's implementation are duplicated, and the new copy won't contain any links to the source from which the copy was made, hence this is a deep copy.

>what's the difference between deep copy and shallow copy or not so deep copy
A shallow copy is copying a pointer. A deep copy is copying the data that the pointer points to. A not so deep copy is a reference count. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.