Assignment operator for a class with reference

Class A
{..........}

Class B:
{....
private U& u;}


I need to write the copy constructor and assignment operator for Class B above
Copy would look something like this
B::B(conts B& bo): u(bo.u){}

is this correct ... and how will the assignment operation look like??
The assignment operator will be whatever you make it to be.

Explain the intended semantics: once you have a B that holds an alias to some outside U, what does it *mean* to replace the contents of this b with another b? Is this action skipping the referenced u (in which case perhaps it should have been a static member?) Is this following the reference to replace the contents of the u as well? It's up to you to decide.

(from design point of view, a class with a non-static member of reference type is probably not supposed to have value semantics in the first place)
Last edited on
yeah that sound logical.. depends on what I want to do in my assignment operation

when an obj1 of class A is assigned to obj2 of class A for non-static member of reference type what would I like to do... I am not sure and don't really know what you mean by not supposed to have value semantics in the first place

could you please elaborate..
To quote the C++ FAQ,
C++ gives you the choice: use the assignment operator to copy the value (copy/value semantics), or use a pointer-copy to copy a pointer (reference semantics). C++ allows you to override the assignment operator to do anything your heart desires, however the default (and most common) choice is to copy the value.

-- http://www.parashift.com/c++-faq/val-vs-ref-semantics.html

Note that adding a non-static reference member disabled the built-in assignment operator. If you don't know what to do, perhaps you don't need one at all? Plenty of types aren't copy-assignable: threads, streams, etc.
Topic archived. No new replies allowed.