Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why we copy a object using copy constructor?
if we not copy an object then what happen and if i copy a object then what happen?
Posted

Google is your friend...
Better than us give long explanations of the differences between shallow and deep copies, etc. you will find more complete information out there in the internet...

Google Search: C++ copy constructor.

More specific links: Link 1 Link 2
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jan-11 14:57pm    
It's may be a right turn of you - my 5.
After long time spend to post my answer to OP's previous question (did you see it?) on default constructors this question thew me into a cognitive dissonance... theory... I say! :-).
fjdiewornncalwe 19-Jan-11 16:07pm    
Thanks
Espen Harlinn 19-Jan-11 15:19pm    
Nice links
fjdiewornncalwe 19-Jan-11 16:07pm    
Thanks to you too...
I think you are overdoing this at this point. This is your 5th or 6th thread on the same topic (I have deleted at least 4 duplicates so far).

If there were no copy constructors at all, you would not be able to call a method that takes non-reference arguments. Nor would you ever be able to assign one object to another. Or in short, the capabilities of the programming language would be very seriously limited.
 
Share this answer
 
Comments
Espen Harlinn 19-Jan-11 15:19pm    
Good answer, a 5+ for the patience :)
Sergey Alexandrovich Kryukov 19-Jan-11 17:00pm    
Another "5" from me. My patience is over, but I would come in again if there is a sensible question.
Nish Nishant 19-Jan-11 17:04pm    
Yeah, I am still not sure if he's serious about these questions.
Sergey Alexandrovich Kryukov 19-Jan-11 19:32pm    
Yes! I was about to ask myself: maybe this is a fine trolling. Why not? OP knows all that casuistic stuff well enough and makes fun of our vanity.

@satyabrat subudhi: Am I right? If so -- my respect! (But you're uncovered.)
I think this is about as "theoretical" as it gets,
read section 12.8 ISO/IEC JTC1 SC22 WG21 N3092[^]

You might be better off reading:
Thinking in C++ 2nd Edition[^] by Bruce Eckel

It's a free electronic book

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
#realJSOP 19-Jan-11 15:02pm    
I thinka simple link would have been sufficient.
Espen Harlinn 19-Jan-11 15:05pm    
Thanks John, he already got his answer here: http://www.codeproject.com/Questions/148123/Copy-Constructor.aspx
Espen Harlinn 19-Jan-11 15:12pm    
And here: http://www.codeproject.com/Questions/147689/Copy-Constructor.aspx - he has repeatedly posted the question ... so I thought the reply appropriate
Espen Harlinn 19-Jan-11 15:15pm    
And Nishant Sivakumar has deleted a couple of duplicates threads too - so thanks a bundle for the univote, something I thought was reserved for a wrong answer
Sergey Alexandrovich Kryukov 20-Jan-11 13:01pm    
I notice down-voting attack... hm, becoming massive, selecting best answers, I guess that's the criterion... (can't vote again :-)
class MyString
{
public:
  MyString(const TCHAR* str){ buff=0; length=0; operator =(str); }

#ifdef USE_COPY_CONSTRUCTOR
  MyString(MyString& copy){ buff=0; length=0; operator = (copy); }
#endif // USE_COPY_CONSTRUCTOR

  ~MyString(){ if(buff) free(buff); }
  
  MyString& operator = (const TCHAR* str){ if(buff) free(buff); buff=_tcsdup(str); len=_tcslen(buff); return *this; }
  operator const TCHAR* () { return buff?buff:__TEXT(""); }

private:
  TCHAR*        buff;
  unsigned int  length;
};


{
  MyString  aaa(__TEXT("aaa"));
  MyString  bbb = aaa;
} // --> crash

#define USE_COPY_CONSTRUCTOR

{
  MyString  aaa(__TEXT("aaa"));
  MyString  bbb = aaa;
} // --> ok

by default members are copied by memcopy.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jan-11 17:03pm    
Well, I already did the coding, more compact, more words -- see OP's previous question -- did not happen much :-)
normally copy const are used when your class contains shareable resources like pointers,handles etc.

In copy constructor of those classes you should create a new instance and pass to the destination object. this is the normal way. it is up to you.

if you are sure about sharing a pointer or handle among objects of your classes and its safe in your application/design, you can live with that.

Hopes it will give you the idea. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900