Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copy Constructors

Copy Constructors

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bhangie
    wrote on last edited by
    #1

    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 ;)

    B D B 3 Replies Last reply
    0
    • B bhangie

      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 ;)

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • B bhangie

        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 ;)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        In addition to Bob's reply, I would suggest replacing the char* member variable with a string or CString 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

        1 Reply Last reply
        0
        • B bhangie

          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 ;)

          B Offline
          B Offline
          bhangie
          wrote on last edited by
          #4

          Ok i will, thanks for the help guys much appreciated. Thanks. Education begins a gentleman, conversation completes him ;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups