Copy Constructors
-
Hi How would i write c++ code to make sure that a deep copy takes place with the following class definition?
class CShop { public: CShop(); ~CShop(); private: CEmployeeContainer* m_pEmployee; char* m_strShopName; };
An explanation will also do rather than code. Thanks in advance. Education begins a gentleman, conversation completes him ;) -
Hi How would i write c++ code to make sure that a deep copy takes place with the following class definition?
class CShop { public: CShop(); ~CShop(); private: CEmployeeContainer* m_pEmployee; char* m_strShopName; };
An explanation will also do rather than code. Thanks in advance. Education begins a gentleman, conversation completes him ;)Hello, Your subject is the answer! Just write a copy constructor that copies all elements of the container and the string.. Example code:
CShop::CShop(const CShop& ShopToCopy) { m_pEmployee = new CEmployeeContainer(); for( CEmployeeContainer::iterator i = ShopToCopy->m_pEmployee->begin(); i != ShopToCopy->m_pEmployee->end(); i++ ) { m_pEmployee->insert(i); } m_strShopName = new char[strlen(ShopToCopy->m_strShopName) + 1]; strcpy(m_strShopName, ShopToCopy->m_strShopName); }
Hope this helps I also got the blogging virus..[^] -
Hi How would i write c++ code to make sure that a deep copy takes place with the following class definition?
class CShop { public: CShop(); ~CShop(); private: CEmployeeContainer* m_pEmployee; char* m_strShopName; };
An explanation will also do rather than code. Thanks in advance. Education begins a gentleman, conversation completes him ;)In addition to Bob's reply, I would suggest replacing the
char*
member variable with astring
orCString
type instead. That's one less memory management detail to have to worry about.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Hi How would i write c++ code to make sure that a deep copy takes place with the following class definition?
class CShop { public: CShop(); ~CShop(); private: CEmployeeContainer* m_pEmployee; char* m_strShopName; };
An explanation will also do rather than code. Thanks in advance. Education begins a gentleman, conversation completes him ;)