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
• 5,020 points
47 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
• 4,960 points

Related Questions In C++

0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11 in C++ by Nicholas
• 5,020 points
18 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
• 4,960 points
90 views
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
• 4,960 points
26 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
• 4,960 points
20 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
206 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,414 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
808 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
636 views
0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

answered Aug 2 in C++ by Damon
• 4,960 points
16 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
• 4,960 points
51 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