标签:struct tor 返回 右值 cpp cout c++ class this
#include <bits/stdc++.h> using namespace std; char cnt = 0; struct A { A() { *reinterpret_cast<char*>(this) = ++cnt; cout << "construct: " << this << endl; } A(int i) { *reinterpret_cast<char*>(this) = i; cout << "construct(" << i << "): " << this << endl; } A(const A &a) { *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a)); cout << "copy construct("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; } A(A &&a) { *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a); cout << "move construct("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; } A operator=(const A &a) { cout << "enter copy assignment" << endl; *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a)); cout << "copy assignment("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; return *this; } A operator=(A &&a) { cout << "enter move assignment" << endl; *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a); cout << "move assignment("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; return *this; } A get_rvalue() { return *this; } void print() const { cout << "[" << this << "," << (int)*reinterpret_cast<char*>(const_cast<A*>(this))<< "]" << endl; } A& get_lvalue() { return *this; } ~A() { cout << "destruct: " << this << endl; } }; void test() { A &&a = -1; cout << &a << endl; } void test2() { A &&a = -1; cout << &a << endl; a = -2; cout << &a << endl; } void test3() { A &&a = -1; cout << &a << endl; (a = -2) = -3; cout << &a << endl; cout << (int)*reinterpret_cast<char *>(&a) << endl; } int main() { test3(); return 0; }
输出结果为:
construct(-1): 0x7ffeeeb3f850 0x7ffeeeb3f850 construct(-3): 0x7ffeeeb3f838 construct(-2): 0x7ffeeeb3f828 enter move assignment move assignment(-2): 0x7ffeeeb3f828->0x7ffeeeb3f850 copy construct(-2): 0x7ffeeeb3f850->0x7ffeeeb3f830 enter move assignment move assignment(-3): 0x7ffeeeb3f838->0x7ffeeeb3f830 copy construct(-3): 0x7ffeeeb3f830->0x7ffeeeb3f840 destruct: 0x7ffeeeb3f840 destruct: 0x7ffeeeb3f830 destruct: 0x7ffeeeb3f828 destruct: 0x7ffeeeb3f838 0x7ffeeeb3f850 -2 destruct: 0x7ffeeeb3f850
改赋值返回右值对象:
#include <bits/stdc++.h> using namespace std; char cnt = 0; struct A { A() { *reinterpret_cast<char*>(this) = ++cnt; cout << "construct: " << this << endl; } A(int i) { *reinterpret_cast<char*>(this) = i; cout << "construct(" << i << "): " << this << endl; } A(const A &a) { *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a)); cout << "copy construct("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; } A(A &&a) { *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a); cout << "move construct("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; } A operator=(const A &a) { cout << "enter copy assignment" << endl; *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a)); cout << "copy assignment("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; return *this; } A&& operator=(A &&a) { cout << "enter move assignment" << endl; *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a); cout << "move assignment("<< (int)*reinterpret_cast<char *>(this) << "): " << &a << "->" << this << endl; return std::move(*this); } A get_rvalue() { return *this; } void print() const { cout << "[" << this << "," << (int)*reinterpret_cast<char*>(const_cast<A*>(this))<< "]" << endl; } A& get_lvalue() { return *this; } ~A() { cout << "destruct: " << this << endl; } }; void test() { A &&a = -1; cout << &a << endl; } void test2() { A &&a = -1; cout << &a << endl; a = -2; cout << &a << endl; } void test3() { A &&a = -1; cout << &a << endl; (a = -2) = -3; cout << &a << endl; cout << (int)*reinterpret_cast<char *>(&a) << endl; } int main() { test3(); return 0; }
输出结果为:
construct(-1): 0x7ffee9665850 0x7ffee9665850 construct(-3): 0x7ffee9665840 construct(-2): 0x7ffee9665838 enter move assignment move assignment(-2): 0x7ffee9665838->0x7ffee9665850 enter move assignment move assignment(-3): 0x7ffee9665840->0x7ffee9665850 destruct: 0x7ffee9665838 destruct: 0x7ffee9665840 0x7ffee9665850 -3 destruct: 0x7ffee9665850
标签:struct tor 返回 右值 cpp cout c++ class this
原文地址:https://www.cnblogs.com/rongminglu/p/12989944.html