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 Constructor and member pointers - Beginner Q.

Copy Constructor and member pointers - Beginner Q.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialloungelearning
3 Posts 2 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.
  • M Offline
    M Offline
    Matthias 0
    wrote on last edited by
    #1

    Hello, I need to write a copy constructor and my class has got a couple of member variables which are pointers. What I need is some general advise on how to write a copy constructor so, that I don't have bad hockey afterwards. Thanks very much! Matthias

    M 1 Reply Last reply
    0
    • M Matthias 0

      Hello, I need to write a copy constructor and my class has got a couple of member variables which are pointers. What I need is some general advise on how to write a copy constructor so, that I don't have bad hockey afterwards. Thanks very much! Matthias

      M Offline
      M Offline
      Michael Welsch
      wrote on last edited by
      #2

      Hi, I would suggest that all classes your class has pointer members to, does also have copy constructors. That way you will keep your copy constructor tidy: Example: class CExClass { ... CExClass(const CExClass& Src) ... CAnyMember* m_pAnyMember; } CExClass::CExClass(const CExClass& Src) { m_pAnyMember = new CAnyMember(*Src.m_pAnyMember); } CAnyMember::CAnyMember(const CAnyMember& Src) { // copy ... } If you don`t want to make real copies of your data, you can use smartpointers as members instead of real pointers. Michael

      M 1 Reply Last reply
      0
      • M Michael Welsch

        Hi, I would suggest that all classes your class has pointer members to, does also have copy constructors. That way you will keep your copy constructor tidy: Example: class CExClass { ... CExClass(const CExClass& Src) ... CAnyMember* m_pAnyMember; } CExClass::CExClass(const CExClass& Src) { m_pAnyMember = new CAnyMember(*Src.m_pAnyMember); } CAnyMember::CAnyMember(const CAnyMember& Src) { // copy ... } If you don`t want to make real copies of your data, you can use smartpointers as members instead of real pointers. Michael

        M Offline
        M Offline
        Matthias 0
        wrote on last edited by
        #3

        thx.

        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