Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Define Copy constructor in C++ code

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 07-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 07-11-2009
Member
 
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.
Reply With Quote
  #4  
Old 07-11-2009
Member
 
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
};
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Define Copy constructor in C++ code"
Thread Thread Starter Forum Replies Last Post
OS 10.5 cannot copy large file error code 0 Max-well Operating Systems 4 27-07-2010 05:16 AM
copy constructor Vs constructor Truster Software Development 5 08-02-2010 02:49 PM
Difference between copy constructor and overloaded assignment operator Solitario Software Development 3 24-12-2009 01:28 PM
Code to copy to clipboard Brunoz Software Development 3 24-11-2009 01:42 PM
Overloading Assignment Operator and Copy constructor in C# Japesh Software Development 0 14-01-2009 03:05 PM


All times are GMT +5.5. The time now is 09:05 AM.