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:

  1. 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.

  2. 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.

  3. 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 need type t{std::addressof(v)}; or use references.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -