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
3 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.
  • S Offline
    S Offline
    Scorpio
    wrote on last edited by
    #1

    Why do i need copy constructor ,in call by value functions.

    N A 2 Replies Last reply
    0
    • S Scorpio

      Why do i need copy constructor ,in call by value functions.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      NiLeSh KoRpE wrote:

      Why do i need copy constructor ,in call by value functions.

      To copy member values of one object to another object of the same type. To make the copied object's state exactly similar to the original. So hence you need a copy constructor coz the compiler needs it during such operations. An eg:

      class CopyCons
      {
      //....Other constructors
      CopyCons(CopyCons**&** ccons)
      {
      //code for copying.
      }
      }

      Note the ampersand in red. It is mandatory for copy constructors that you pass the object to be copied by reference or else it will result in calling the copy constructor again for the same object leading to trouble.


      Nibu thomas Software Developer Faqs by Michael dunn

      1 Reply Last reply
      0
      • S Scorpio

        Why do i need copy constructor ,in call by value functions.

        A Offline
        A Offline
        Alton Williams
        wrote on last edited by
        #3

        Nibu thomas wrote:

        Note the ampersand in red. It is mandatory for copy constructors that you pass the object to be copied by reference or else it will result in calling the copy constructor again for the same object leading to trouble.

        Because it calls by value which makes a copy of the object which a copy constructor does so you'll get an infine call to a copy constructor, which will hang your system!

        NiLeSh KoRpE wrote:

        Why do i need copy constructor ,in call by value functions.

        NEVER! a copy constuctor and is supplied by the compiler it should be used when you have a property that allocates memory. in in such a case it is beneficial to overide the assignment operator. And a destructor (C++ does not do automatic memory management, like Java). Also another another constructor (it's got to copy something) e.g.

        class foo{
        public:
           foo(); //default ctor
           ~foo(); //dtor
           foo(const foo &source); //copy ctor
           foo& operator =(const foo &source); //overloading assingment operator
        private:
           baz *m_data; // pointer to sometjhing of type baz
        };
        
        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