943,740 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9851
  • C++ RSS
Jul 28th, 2004
0

need the usage of Copy Constructor

Expand Post »
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.....
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
unirs_1972 is offline Offline
1 posts
since Jul 2004
Jun 22nd, 2005
0

Re: need the usage of Copy Constructor

Let's take an example of class A
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
e215774 is offline Offline
3 posts
since May 2005
Jun 22nd, 2005
0

Re: need the usage of Copy Constructor

> Can you please provide me with examples of when and how copy constructors are used..
The simplest example is passing an object by value:
C++ Syntax (Toggle Plain Text)
  1. class C {};
  2.  
  3. void foo(C obj); // C's copy constructor is called
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jun 23rd, 2005
0

Re: need the usage of Copy Constructor

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
e215774 is offline Offline
3 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C/c++
Next Thread in C++ Forum Timeline: Plz help......Map-coloring problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC