Solved

copy constructor and assignment opearator

Posted on 2004-09-09
6
319 Views
Last Modified: 2010-04-01
Hello
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.
Regards
0
Comment
Question by:Mtnl
6 Comments
 
LVL 9

Expert Comment

by:jhshukla
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{
 public:
  int *ptr;
  int sz;
};

int main(){
  class1 c1, c2;
  c1.sz = 16;
  c1.ptr = new int[c1.sz];
  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.

jaydutt
0
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 50 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.

example:

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.
0
 

Author Comment

by:Mtnl
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.
thanks

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Expert Comment

by:s_senthil_kumar
ID: 12029517
Mtnl,

  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{
 public:
  int *ptr;
  int sz;
~A()
{
   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.
0
 

Author Comment

by:Mtnl
ID: 12033542
Thanks Jksukla and pankaj.
0
 

Author Comment

by:Mtnl
ID: 12033547
and thanks to senthil kumar also
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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