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

Copy Constructor

Scheduled Pinned Locked Moved C / C++ / MFC
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.
  • R Offline
    R Offline
    ragavan
    wrote on last edited by
    #1

    Hi I am having the following doubt regarding copy constructor E.g If I have class A then the copy constructor will be A(const A& x) { } why it can't be 1. A(const A x) { } 2. A(const A* x) { } Please give me the reason also

    S N T 3 Replies Last reply
    0
    • R ragavan

      Hi I am having the following doubt regarding copy constructor E.g If I have class A then the copy constructor will be A(const A& x) { } why it can't be 1. A(const A x) { } 2. A(const A* x) { } Please give me the reason also

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      ragavan wrote:

      1. A(const A x) { }

      If the argument x is passed by value a copy is needed. This is the job of the copy constructor. Therefore we'd need to call the copy constructor to copy the argument to the copy constructor, and so on, endlessly recursively.

      ragavan wrote:

      2. A(const A* x) { }

      Because an object itself need copying, not the object indirected through a pointer. It could have worked this was but it doesn't make as much sense.

      Steve

      1 Reply Last reply
      0
      • R ragavan

        Hi I am having the following doubt regarding copy constructor E.g If I have class A then the copy constructor will be A(const A& x) { } why it can't be 1. A(const A x) { } 2. A(const A* x) { } Please give me the reason also

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        From the C++ Standard ISO/IEC 14882-1998 12.8/2: A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.


        Programming Blog utf8-cpp

        1 Reply Last reply
        0
        • R ragavan

          Hi I am having the following doubt regarding copy constructor E.g If I have class A then the copy constructor will be A(const A& x) { } why it can't be 1. A(const A x) { } 2. A(const A* x) { } Please give me the reason also

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          ragavan wrote:

          Please give me the reason also

          interview question ahha :)

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          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