GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jan-2006, 04:38
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Need of assignment operator in case of copy constructor


I always come across this statement...that you should also define assignment operator when you define copy constructor.
I don't think its any rule....can anyone elaborate on this...
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
  #2  
Old 01-Jan-2006, 05:09
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need of assignment operator in case of copy constructor


Happy New Year.. (the second time!)
Quote:
Originally Posted by alcoholic
I always come across this statement...that you should also define assignment operator when you define copy constructor.
I don't think its any rule....can anyone elaborate on this...
Its not a rule. But it results in faulty programs...

And this is applicable only if you have dynamic memories associated with the class.

I Hope you know the difference between assignment and initialization.
Copy constructor initializes the object, whereas, assignment means making one object equal to the other.

Consider this case:
CPP / C++ / C Code:
int a, b=10;
a = b;

So, b is initialized to 10, whereas a is assigned.

This is similar to objects.
Consider that there is a class A, having a character pointer.
(char* is dynamically allocated!! this is the most important thing...)
CPP / C++ / C Code:
class A {
    char *x;
    int length;
public:
    void initialize();
    ~A();
};

Now, consider this in the main function:
CPP / C++ / C Code:
A obj1;
obj1.initialize();

A obj2 = obj1;    // Copy constructor, Initialization

A obj3;
obj3 = obj1;       // Assignment


So, what has happened?
obj2 has called the copy constructor.
If the compiler doesnt find one, it generates the default one-to-one copy constructor for you.

Heck! there lies the error!
Now, both obj2.x and obj1.x points to the same location!!!!

To resolve this, you should explicitly define a copy constructor like this:
CPP / C++ / C Code:
    A( const A &y) {
        length = y.length;
        x = new char[length+1];
        strcpy(x, y.x);
    }
and you should properly allocate the memory.

But what if we perform assignment?
that is the case of the obj3 object.
The compiler automatically generates a default operator=, and again, we get the same error.
So, we have to explicitly define assignment operator like the same way we did for the copy constructor.
Like this:
CPP / C++ / C Code:
void operator=(const A &y) {
        length = y.length;
        x = new char[length+1];
        strcpy(x, y.x);
}
Now, the problem is solved.

Hope This Helps,
Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 01-Jan-2006, 07:15
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: Need of assignment operator in case of copy constructor


Thanx again...I was thinking in this line but was not sure...you made it clear
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to listens to two different ports at the same time jaro C Programming Language 5 06-Jan-2006 07:36
Problem with one variable bretter C++ Forum 1 16-May-2005 08:20
Compiling Errors ToddSAFM C++ Forum 22 18-Dec-2004 12:42
there has to be a better way dabigmooish C++ Forum 8 17-May-2004 11:24
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 18:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.