Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++MFC
Hi,
 
I had gone through number of articles, but unable to get any concrete idea on both.
 
I mean to say -
- Why we need [practical usage] these two?
- Is there any substitution to these two?
- When it is compulsory to use them?

Kindly please guide me in detail (Practical usage\main advantages).
 
Thanks
Posted 1-Jun-11 20:35pm
pjhelp515
Edited 1-Jun-11 22:01pm
CPallini296.3K
v3
Comments
Dalek Dave at 2-Jun-11 3:40am
   
Edited for Grammar and Readability.
CPallini at 2-Jun-11 4:00am
   
You should read instead a good C++ book.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

We need Copyconstructor to initialize one object from other object, And generally we use assignment operator to invoke copy constructor.
  Permalink  
Comments
Dalek Dave at 2-Jun-11 3:41am
   
Good answer.
Laxmikant_Yadav at 2-Jun-11 4:42am
   
Thanks :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I Think was explained lot of times in CP.But not by me so i am taking this one,
A copy constructor is a special constructor that initializes a new object from an existing object.Compiler will creates a default copy constructor which copies your class data bit by bit, if you wont create it.Then why we have to re-create a Copy constructor?consider this class(not a professional code).
Class A
{
    public int *p;
    A(){ p = new int;}
    ~A(){
         delete p;
     }
};
int main()
{
   A a;
   A b = a; //default copy constructor provided by compiler,which exactly copies a.p pointer to to b.p;
   return 0;
};
But there is one big problem.when main is going to die,it will clean all stack variables.So may be first "a" destructor will be called and "a.p" is deleted.Next b destructor will be called,which again tries to delete b.p which is already deleted in a(Note thst a.p and b.p pointes same pointer).This will cause an run time error.To overcome this problem we have to create a copy aonstructor and Assignement operator(which is called in different scenarios).So the Class A copy constructor will be,
class A::A(const class A& RefA)
{
   p = new int;    // create a new p 
   *p = *RefA.p; // copy the value
}
So now both a, b in the above example will have different p pointers.Assignement operator functionality is also same.But there aim is defferent.
 
There are three general cases where the copy constructor is called instead of the assignment operator:
1. When instantiating one object and initializing it with values from another object (as in the example above).
2. When passing an object by value.
3. When an object is returned from a function by value.
 
In other cases like,
A a;
a = b; 
an Assignment operator is called
Comments
HarishTheLearner at 2-Jun-11 3:19am
   
kudos!My 5
venkatmakam at 2-Jun-11 4:51am
   
Thanks Harish
Dalek Dave at 2-Jun-11 3:41am
   
Good Call.
venkatmakam at 2-Jun-11 4:51am
   
Thank you.
Laxmikant_Yadav at 2-Jun-11 4:45am
   
Good answer
venkatmakam at 2-Jun-11 4:51am
   
Thanks
S Mewara at 2-Jun-11 10:43am
   
My 5!
venkatmakam at 2-Jun-11 11:26am
   
Thank you:)
pjhelp at 2-Jun-11 14:33pm
   
Thank U very much Venkat :)
venkatmakam at 3-Jun-11 1:30am
   
No problem :)
Albert Holguin at 2-Jun-11 19:52pm
   
great answer, my 5
venkatmakam at 3-Jun-11 1:29am
   
Thank you
Member 8576081 at 27-Jun-12 5:42am
   
I know I am a bit late, but could not help giving it a 5.Explained in an amazingly simple manner.Cheers!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi venkat
 
thanks for assisting laxmikanth ( actually you saved my lot of time of typing and explaining your case when we need Copy constructor / assignmnet oprator )
 
Venkat has explained the case of Deep Copy ( where we need to take a copy of object which has pointer datamember )
 
Another need of Copy constructor / assignment operator comes when we are designing our class as "SingleTon"
 
SingleTon: a class which can have only one exclusive object thrughout the application session
 
so in the case when our class should not allow 2nd copy of object then we have to override Copy constructor / assignment operator so that client can not create another instance of the call by copying/assining the abject to a new one
 
Thank You
Sanjay
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To override the copy constructor / assignment operator to stop the creation of another instance of the class place them in the private part of your class, and leave out the body.
class A {
    // Our copy constructor is private
    A(const A&);
    // Our assignment operator is private
    A& operator=(const A&);
public:
    // Default constructor
    A() {}
    // Destructor
    ~A() {}
};
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 468
2 Abhinav S 305
3 sanket saxena 295
4 Tadit Dash 160
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid