need the usage of Copy Constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 1
Reputation: unirs_1972 is an unknown quantity at this point 
Solved Threads: 0
unirs_1972 unirs_1972 is offline Offline
Newbie Poster

need the usage of Copy Constructor

 
0
  #1
Jul 28th, 2004
Hi everyone
I am new to C++ and I have read abt copy constructors from a C++ book but could not exactly get the concept. Can you please provide me with examples of when and how copy constructors are used..



Thanks in advance.....
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: e215774 is an unknown quantity at this point 
Solved Threads: 0
e215774 e215774 is offline Offline
Newbie Poster

Re: need the usage of Copy Constructor

 
0
  #2
Jun 22nd, 2005
Let's take an example of class A
  1. class B //With copy constructor
  2. {
  3. private:
  4. char *name;
  5. public:
  6. B()
  7. {
  8. name = new char[20];
  9. }
  10. ~B()
  11. {
  12. delete name[];
  13. }
  14. //Copy constructor
  15. B(const B &b)
  16. {
  17. name = new char[20];
  18. strcpy(name, b.name);
  19. }
  20. };
<< moderator edit: added [code][/code] tags >>

Let us Imagine if you don't have a copy constructor for the class B. At the first place, if an object is created from some existing object, we cannot be sure that the memory is allocated. Also, if the memory is deleted in destructor, the delete operator might be called twice for the same memory location.
This is a major risk. One happy thing is, if the class is not so complex this will come to the fore during development itself. But if the class is very complicated, then these kind of errors will be difficult to track.

Thanks,
Vivek
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: need the usage of Copy Constructor

 
0
  #3
Jun 22nd, 2005
> Can you please provide me with examples of when and how copy constructors are used..
The simplest example is passing an object by value:
  1. class C {};
  2.  
  3. void foo(C obj); // C's copy constructor is called
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: e215774 is an unknown quantity at this point 
Solved Threads: 0
e215774 e215774 is offline Offline
Newbie Poster

Re: need the usage of Copy Constructor

 
0
  #4
Jun 23rd, 2005
There are 3 important places where a copy constructor is called.

When an object is created from another object of the same type
When an object is passed by value as a parameter to a function
When an object is returned from a function
If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow copy. If the class does not have pointer variables with dynamically allocated memory, then one need not worry about defining a copy constructor. It can be left to the compiler's discretion.

But if the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.

As i given an example in prev reply.
Thanks,
Vivek
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC