Is it UB if const member function changes `*this` through its argeument?
Does this code exhibit undefined behavior or is the behavior well-defined? The const member function promises that it will not change *this, but it actually changes *this through the argument. Can it output 1? #include <iostream> class A { public: A(int n) : n_(n) {} void DoubleTo(A& a) const { a.n_ = 2 * n_; std::cout << n_ << "\n"; } private: int n_ = 0; }; int main() { A a(1); a.DoubleTo(a); }
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.