Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


copy constructor and assignment opearator

Posted on 2004-09-09
Medium Priority
Last Modified: 2010-04-01
Compiler give copy constructor and constructor implicitly. then what the need of declare copy constructor explicitly.
Suppose we have three different scnerio
void func(int ii);
void func(int &ii)
void func(int *ii)
In which case copy constructor call and why.
Please also tell me the difference between Assignment operator and copy constructor.
How the Shallow copy and bit copy work.
Question by:Mtnl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 12023714
you need a copy ctor when you have pointer members in your class. the default copy ctor provided by the compiler clones your variable. if you are dealing with dynamic mem allocation this could and in most cases would cause problems. here's an example

class class1{
  int *ptr;
  int sz;

int main(){
  class1 c1, c2; = 16;
  c1.ptr = new int[];
  c2 = c1;
  delete[] c1.ptr;
  return 0;
c2.ptr is now pointing to a location in heap which is no longer yours. if you try to access it a few lines later you would probably get garbage or a crashed program. in such a case you would want to write a copy ctor that would: (1) copy sz variable, (2) allocate new memory, (3) and use a loop to copy the array. and don't forget to write the destructor or there would be memory leaks.


Accepted Solution

pankajtiwary earned 150 total points
ID: 12023802
There are three things constructor, copy constructor and assignment operator. You need to remember the following things.

1. The constructor is called whenever you are trying to create an object from *scratch*. That means you are going to create a fresh new piece of something. Then comes the default constructor. A default constructor is something that can be called without any arguments. This can be of 2 types. Either the argument is not necessary at all. This is of the form Myclass(). The other form may be that the arguments are necessary but you have provided the constructor woth defaulkt parameters. This is of the form MyClass(int a=0, int b=0). Both of these are default constructor. The rule is if you don't provide any *constructor* for the class, the compiler will create one for you. That will be of the form MyClass(). But if you define even one constructor, the comiler won't create any for you.


Myclass a;
Myclass b(1,2);

These both lines will call the constructor.

2. The copy constrcutor is called whenever you are trying to create a new object from an existing one or you need to create a copy of an object.

MyClass c = b;

The above statement will call the copy constructor because c is being created from b. Also if you are passing or returning an object by value then also a copy of the object is created hence copy constructor is called.

3. The assignment operator is called when you have 2 fully made objects and you want to assign one to the other.

b = a;

This will invoke the assignment operator.

Just liek default constructor the compiler provides one copy constructor which does nothing but a bitwise copy. If you want your control over the copying process you need to define your own copy constructor. Your copy constructor will simulate the bahaviour of creating a fresh new object from the existing one.

Hope this helps.

Author Comment

ID: 12027277
thanks pankaj
As you said if you not specify any copy constructor, compiler will give copy constructor for you.If compiler is giving implicitly, what the need of define copy constructor explicitly. And plz also discuss this three case......
void func(int ii);
void func(int &ii)
void func(int *ii)
 when copyconstructor or constructor  will called and why.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 12029517

  You need to write your own copy constructor if the compiler generated one doesn't do the right thing. Take jkshukla's example.
class A{
  int *ptr;
  int sz;
   delete ptr;

Now suppose you have code like this.
void Foo()
A objClass1;
       A objClass2 = objClass1;

objClass2 is copy constructed and because you've not defined a copy constructor, the compiler generated one does a bitwise copy, it copies values of ptr and sz from objClass1 to objClass2.
Now when objClass2 goes out of scope, its destructor gets called, which deletes ptr. But objClass1 is still alive and might access ptr. Deferencing a deleted pointer will lead to undefined behaviour, as you surely must be knowing.

So what you do is define a copy constructor yourself that does the right thing. The easiest thing would be to allocate memory and copy contents.

void func(int ii);
void func(int &ii)
void func(int *ii)

Copy constructors are not involved here, as they are only relevant for structs/classes. For those two, passing by value will involve copy construction, passing by reference/pointer will not.

Author Comment

ID: 12033542
Thanks Jksukla and pankaj.

Author Comment

ID: 12033547
and thanks to senthil kumar also

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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