assigning^constructing a c++obj: a few diff

Assignment operator is a field-by-field and bitwise overwrite [1] without eating up new memory. Both LHS and RHS must be (preexisting) objects with addresses.

A constructor always eats up (heap or stack) new memory, and reduces free memory.

[1] If a field is a pointer, then the same address is put into the LHS’s field bitwise. LHS and RHS both point to the same address — shallow copy.

Q: what if a field is a reference?
A: Can’t reassign. Default op= will trigger an error (compile time or runtime).
A: reference field is rare.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s