Results 1 to 4 of 4

Thread: Define Copy constructor in C++ code

  1. #1
    Join Date
    Aug 2009
    Posts
    155

    Define Copy constructor in C++ code

    I am a new user to C++ programming. I have recently started using this programming language and I am finding it quite interesting to work on. I am not that well versed with C++ programming language. I have come across a term Copy Constructor. I know what a constructor is. But I want to know what are Copy Constrctors in C++? Can anyone explain it to me ?
    Last edited by Shaan12; 07-11-2009 at 04:39 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Define Copy constructor in C++ code

    Yes, C++ is a really good programming language. I am also working on the same. C++ allows you to create a copy of an already existing object. A copy constructor in C++ is a special constructor which lets you create a new object as a copy of an already existing object. Copy constructor's first argument is a reference to an object of same type. This then might be followed by parameters of any type.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Define Copy constructor in C++ code

    C++ inhabits a very unique constructor called as Copy Constructor. Whenever we create a new variable from an object the Copy Constructor is called. A variable is declared which is initialized from another object in the following manner:

    Car a("Civic"); // constructor is used to build a.
    Car b(c); // copy constructor is used to build b .
    Person c = a; // copy constructor is used to initialize in declaration.

    In case of assignment a Copy constructor is not created eg.
    c = a; // Assignment operator, no constructor or copy constructor.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Define Copy constructor in C++ code

    The basic intension of C++'s Copy Constructor is to initialize an object of a given class by copying an existing object of that very class. C++ compiler does create a Copy constructor even though you don't create it. But this would only do a shallow copy of the object. But developers need to create a Deep Copy of the Object, for this they make use of Copy Constructor.

    eg.

    Code:
    Class temp
    {
    Temp();
       Temp(Temp&);           // copy constructor
    };

Similar Threads

  1. OS 10.5 cannot copy large file error code 0
    By Max-well in forum Operating Systems
    Replies: 4
    Last Post: 27-07-2010, 05:16 AM
  2. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  3. Replies: 3
    Last Post: 24-12-2009, 01:28 PM
  4. Code to copy to clipboard
    By Brunoz in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 01:42 PM
  5. Overloading Assignment Operator and Copy constructor in C#
    By Japesh in forum Software Development
    Replies: 0
    Last Post: 14-01-2009, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,132,798.60400 seconds with 16 queries