Sunday, July 12, 2009

C++: What's the diference between overloading the = operator and a copy constructor?

Copy constructor is called whenever an object is copied and new object has to be created with the same data. This happens when you say foo = bar, but also when you pass the object as a parameter to a function by value, i.e calling foo(bar) where bar isn't a reference or pointer.





The = operator can also handle other types like assigning an int to your object, or a different object to your object. Like you can do this by overloading the = operator but not with the copy constructor.





int foo;


YOUROBJECT bar;


bar = foo;


No comments:

Post a Comment