Is this an example of reference reassignment? C++11 -
Is this an example of reference reassignment? C++11 -
as understand it, 1 cannot alter reference variable 1 time has been initialized. see, instance, this question. however, here minmal working illustration sort of reassign it. misunderstanding? why illustration print both 42 , 43?
#include <iostream> class t { int x; public: t(int xx) : x(xx) {} friend std::ostream &operator<<(std::ostream &dst, t &t) { dst << t.x; homecoming dst; } }; int main() { auto t = t(42); auto q = t(43); auto &ref = t; std::cerr << ref << std::endl; ref = q; std::cerr << ref << std::endl; homecoming 0; }
that not perform reference reassignment. instead, copy assigns object in variable q
object referenced ref
(which t
in example).
this justifies why got 42 output: default re-create assignment operator modified first object.
c++11 reference
Comments
Post a Comment