[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

copy initialization and assignment

Posted on 1999-12-20
2
Medium Priority
?
281 Views
Last Modified: 2010-04-10
What is the reason for using a copy constructor?Is it used
only to avoid copying memory addresses while initialization
and assignment?
             What is the relation between copy constructors and
copy initialization.please explain how is the copy constructors
are called in programs?
0
Comment
Question by:kesavan_sridhar
2 Comments
 

Accepted Solution

by:
jelaiw earned 60 total points
ID: 2296998
You should probably refer to your textbook, but in a nutshell you need to write your own copy constructor (C++ provides one by default) for a class whenever you use dynamic memory (also known as heap memory).

The problem arises when you have two copies of an object (each of which contains a pointer into the same heap memory, because they are copies remember!) and one of them passes out of scope and its resources are deallocated.  Poof, the resource is now gone, but wait, there is now a pointer (in the other object) still thinking it is pointing to valid memory.

A copy constructor specifies to C++ how you *really* want to make a copy of the object, obviously with it's own pointer.

Copy assignment works much in the same way, you want a copy of what is being assigned, not a pointer to the other objects variable.

An explicit call to a copy constructor looks like this:

SomeClass SomeObject;
SomeClass AnotherObject(SomeObject);

another object is now a copy of some object.  C++ automatically calls the copy constructor when it needs to.

Hope this helps,

jw

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2297116
Two notes:

>> whenever you use dynamic memory
Goes for other resources as well

>> SomeClass SomeObject;
>> SomeClass AnotherObject = SomeObject;
Will also use the copy constructor and NOT the assignment operator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question