| | |
need the usage of Copy Constructor
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
Let's take an example of class A
<< 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
C++ Syntax (Toggle Plain Text)
class B //With copy constructor { private: char *name; public: B() { name = new char[20]; } ~B() { delete name[]; } //Copy constructor B(const B &b) { name = new char[20]; strcpy(name, b.name); } };
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
> Can you please provide me with examples of when and how copy constructors are used..
The simplest example is passing an object by value:
The simplest example is passing an object by value:
C++ Syntax (Toggle Plain Text)
class C {}; void foo(C obj); // C's copy constructor is called
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Copy constructor in derived class (C)
- the common usage of a copy constructor (C++)
- copy constructor problem (C++)
Other Threads in the C++ Forum
- Previous Thread: C/c++
- Next Thread: Plz help......Map-coloring problem
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





