Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copy constructor and assignment opearator

Posted on 2004-09-09
6
Medium Priority
?
353 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 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.

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

782 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