c++ - Why is the argument of the copy constructor a reference rather than a pointer? -
why argument of copy constructor reference rather pointer?
why can't use pointer instead?
there many reasons:
references cannot null. ok, it's possible create null reference, it's possible cast
std::vector<int>*
std::vector<sometype>*
. doesn't mean such cast has defined behavior. , neither creating null reference. pointers have defined behavior when set null; references not. references therefore expected refer actual objects.variables , temporaries cannot implicitly converted pointers types. obvious reasons. don't want pointers temporaries running around, why standard expressly forbids doing (at least when compiler can tell doing it). allowed have references them; these implicitly created.
because of point number 2, using pointers rather references require every copy operation use address-of operator (&). oh wait, c++ committee foolishly allowed overloaded. copy operation need use
std::addressof
, c++11 feature, address. every copy needtype t{std::addressof(v)};
or use references.
Comments
Post a Comment