why are copy constructors needed and what are the cases where they are very helpful

0 votes
Why do we require copy constructors?

 

I'm learning C++ and I'm not sure why copy constructors are necessary, because I was getting correct results without them as well.

I went through a few examples, but it seemed to me that having copy constructors, such as to initialise variables, is just good practise.

Could someone please explain the concept of copy constructors to me?

Any assistance will be greatly appreciated.

Thank you very much.
May 28 in C++ by Nicholas
• 2,460 points
34 views

1 answer to this question.

0 votes
A copy constructor is a member function that uses another object of the same class to initialize an object.

When is it appropriate to write our own copy constructor?

The C++ compiler includes a copy constructor (and assignment operator) by default with each class.

When we don't provide an implementation of the copy constructor (and assignment operator) and attempt to initialize an object with an already initialized object of the same class, the copy constructor is called and copies class members one by one in the target object.

However, the default copy constructor (and assignment operator) has the following flaw:

When we have members that are dynamically initialized at run time, the default copy constructor copies this member with the address of dynamically allocated memory rather than making a true copy of this memory.

Both objects now point to the same memory, and changes in one are mirrored in the other.
answered May 31 by Damon
• 3,780 points

Related Questions In C++

0 votes
1 answer

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
9 views
0 votes
1 answer

What is the name of the "<<" and ">>" operators? [duplicate]

According to cplusplus.com's documentation: This operator (<<) applied to ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
7 views
0 votes
1 answer

C++ code file extension? What is the difference between .cc and .cpp [closed]

GNU GCC recognizes all of the following ...READ MORE

answered Jun 21 in C++ by Damon
• 3,780 points
4 views
0 votes
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

answered Jun 6 in C++ by pranav
• 2,580 points
21 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
190 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,373 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
787 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,720 points
585 views
0 votes
1 answer

What is the difference between std::__gcd and std::gcd?

I done some research about this. The ...READ MORE

answered Jun 10 in C++ by Damon
• 3,780 points
16 views
0 votes
1 answer

Where are the Microsoft Visual C++ 2015-2022 Redistributable (x64) packages installed?

They are, in my opinion, at their best. C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC It's worth noting ...READ MORE

answered Jun 15 in C++ by Damon
• 3,780 points
18 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP