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 Constuctor

Copy Constuctor

Scheduled Pinned Locked Moved C / C++ / MFC
javaquestion
4 Posts 4 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.
  • T Offline
    T Offline
    tom groezer
    wrote on last edited by
    #1

    Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?

    F B D 3 Replies Last reply
    0
    • T tom groezer

      Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?

      F Offline
      F Offline
      fefe wyx
      wrote on last edited by
      #2

      The parent constructor is called at the beginning of the initialization, but not necessarily the copy constructor. For a default copy constructor, of course the copy constructor of the base would be called. But if the copy constructor is provided in the derived class explicitly, it can be chosen that which one of the base class's constructor is to be called.

      1 Reply Last reply
      0
      • T tom groezer

        Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?

        B Offline
        B Offline
        bob16972
        wrote on last edited by
        #3

        tom groezer wrote:

        Does it have to be explicit?

        The best advice is to always make it explicit to avoid surprises. It can get mundane for simple classes but helps prevent some of life's little oopsies when your class manages resources. As far as I know, if you explicitly implement the copy constructor, you should call the super class copy constructor first in your initilization list. Also remember to call the super class assignment operator in it's explicit implementation to prevent any surprises. It is best to define the copy constructor and the assignment operator to avoid what may or may not get done by the default implementations. If you have "Effective C++ Third Edition" by Scott Meyers, there is some great, to the point, advice/information on this topic. If you don't have it, it's one of the few books that can justify the $40+ price tag.

        1 Reply Last reply
        0
        • T tom groezer

          Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?

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

          tom groezer wrote:

          Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called?

          Why not set a breakpoint in both, run the program, and note which breakpoint is hit first?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          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