By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

overloaded assignment operator and copy constructor.

P: n/a
Hi Everyone,

I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,

obj1 = obj;

so the overloaded operator function performs the necessary copy of
obj's member in to obj1. Can't the same be done using copy
constructor? If so, then what is the difference between both the
mechanisms.

Thanks in advance!!!

Aug 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 22, 3:56 pm, sam_...@yahoo.co.in wrote:
Hi Everyone,

I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,

obj1 = obj;

so the overloaded operator function performs the necessary copy of
obj's member in to obj1. Can't the same be done using copy
constructor? If so, then what is the difference between both the
mechanisms.

Thanks in advance!!!
assignment operator is "assignment operator", copy constructor is a
"constructor". both have different semantics.

class X { };

int main()
{
X x;
X y = x; //invokes copy constructor, since y is getting constructed
y = x; // invokes copy assignment operator, since y already exists
and is merely getting assigned.
}

-N

Aug 22 '07 #2

P: n/a
In message <11*********************@q3g2000prf.googlegroups.c om>,
Neelesh Bodas <ne***********@gmail.comwrites
>On Aug 22, 3:56 pm, sam_...@yahoo.co.in wrote:
>Hi Everyone,

I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,

obj1 = obj;

so the overloaded operator function performs the necessary copy of
obj's member in to obj1. Can't the same be done using copy
constructor? If so, then what is the difference between both the
mechanisms.

Thanks in advance!!!

assignment operator is "assignment operator", copy constructor is a
"constructor". both have different semantics.

class X { };

int main()
{
X x;
X y = x; //invokes copy constructor, since y is getting constructed
y = x; // invokes copy assignment operator, since y already exists
and is merely getting assigned.
Not always "merely". Assignment also involves disposing of the previous
contents.
>}

-N
--
Richard Herring
Aug 22 '07 #3

P: n/a
On Aug 22, 1:56 pm, sam_...@yahoo.co.in wrote:
Hi Everyone,

I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,

obj1 = obj;

so the overloaded operator function performs the necessary copy of
obj's member in to obj1. Can't the same be done using copy
constructor? If so, then what is the difference between both the
mechanisms.

Thanks in advance!!!
constructors are -say philosofically- invoked on newly allocated
objects who do not carry any valid data, but assignment happens for
previously constructed objects that contain valid -though not
interesting - data. Thus an assignment operator might need to get read
of rubish data in some cases -often pointeriod types - while
constructors generally need not destroy anything before copying
existing data to a new location.

regards,
FM.

Aug 22 '07 #4

P: n/a
MyType& operator=( MyType const& other )
{
MyType temporary( other ); // Copy construct into new instance.
swap( other ); // Swap contents with new instance.

Sorry, I cannot understand what does it mean swap?

For example, while cannot we do:

MyType& operator=(MyType const& other)
{
swap(other);
return *this;
}

Please, can you describe (for example) such situation:

class MyClass
{
int* pArray; /pointer to array, need allocation memory
/...
}

what's copy constructor, operator= and swap?

Aug 24 '07 #5

P: n/a
On Aug 25, 12:19 am, alexdem...@gmail.com wrote:
MyType& operator=( MyType const& other )
{
MyType temporary( other ); // Copy construct into new instance.
swap( other ); // Swap contents with new instance.

Sorry, I cannot understand what does it mean swap?

For example, while cannot we do:

MyType& operator=(MyType const& other)
{
swap(other);
return *this;

}

Please, can you describe (for example) such situation:

class MyClass
{
int* pArray; /pointer to array, need allocation memory
/...

}

what's copy constructor, operator= and swap?
The term 'swap' means exchanging the values of two objects.A general
solution for swappig two objects is the famuse triple assign method:

temp=other;
other=me;
me=temp;

but in some cases there exists a short cut and we do not need to pay
that much for the swap;In such cases a swap is normally performed much
faster and easier than an assignment ,and assignment is defined in
terms of swap rather than the vice versa.(pointeroids , string &
reference counting are best examples .):

struct my_string{
public:
my_string():str(null),sz(0){};
my_string(const char *const s){/* build from null-terminated c-
style string and literals*/
sz=strlen(s);
str=new char[sz];
memcpy(str,s,sz);
};
my_string(my_string& s):str(new char[s.sz]),sz(s.sz)
{memcpy(str,s.str,sz);};

bool swap(my_string& right){
if (right==*this) return false;

//swap str:
char* temp=str;
str=right.str;
right.str=temp;

//swap sz:
sz^=right.sz;
right.sz^=sz;
sz^=right.sz;

return true;
};

bool operator==(const my_string& right){
if(sz!=right.sz) return false;
return 0==memcmp(str,right.str,sz);
};

const my_string& operator=(my_string other){
swap(other);
return *this;
};

~my_string(){delete[] str;};

private:
char* str;
size_t sz;
....//concate , find , cstr , size ... etc
};

this is not the best approach to design strings but it shares the
common swap/copy/assign method of dynamic memory management with
better solutions.If you defined swap in terms of assignment,allocation/
deallocation(new/delete) of memory would happen three times per
swap.But now swap needs no allocation/deallocation and as you can
see,assignment needs exert a deallocation in either case (destruction
of auto variable or explicit delete).

regards,
FM

Aug 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.