a = b;Where a and b are objects of the same class. It is not a constructor but the problems with non POD data are similar. You'll usually find that books on C++ classes refer to assignment operators at the same time as copy constructors. An assignment operator is defined as a non static member function with just one parameter.
Assignment versus Initialization Initialization means creating a new variable and then doing an assignment of a value to it so the only difference is the creation part of initialization. Assignment always works with a variable that exists. If we think in ints then the example below shows examples of assignment, copying and initialization.
int a= 9; // initializationWith objects, the same is true, only assignment and copying can be more complicated if pointers or other non POD objects is involved. BTW, a pointer can be POD if it points to const data or a member function but it's not if new was used.
a =10; // assignment
int b=4; // initialization
a = b; // copying
The assignment operator is another method that the compiler will add for you if you don't supply one yourself. The definition of an assignment operator in class A is
A& operator= (const A& x) throw() ;In our phonerec example, we might code it like this:
phonerec & operator=(const phonerec & rhs) throw() {You might wonder why this returns a reference to an object of the same class. It isn't strictly needed - returning void would work ok and you could drop the return *this in that case but it is a convention and it is needed if you ever write multiple assignments like this:
name= rhs.name;
phonenumber = rhs.phonenumber;
return *this;
}
c = b = a;
On the next page An example of assignment Operators

