码迷,mamicode.com
首页 > 其他好文 > 详细

cpp test

时间:2020-05-29 23:40:29      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:value   bsp   bit   highlight   cout   return   err   inter   pre   

#include <bits/stdc++.h>
using namespace std;

char cnt = 0;

struct A {
    A() {
        *reinterpret_cast<char*>(this) = ++cnt;
        cout << "construct: " << this << endl;
    }
    A(const A &a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a));
        cout << "copy construct: " << &a << "->" << this << endl;
    }
    A(A &&a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a);
        cout << "move construct: " << &a << "->" << this << endl;
    }
    A operator=(const A &a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a));
        cout << "copy assignment: " << &a << "->" << this << endl;
        return *this;
    }
    A operator=(A &&a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a);
        cout << "move assignment: " << &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;
    }
};

int main()
{
    A a;

    A &&b = a.get_rvalue();
    const A &&c = a.get_rvalue();
    // A &d = a.get_rvalue(); // error
    const A &e = a.get_rvalue();
    cout << "-" << endl;

    a.print();
    b.print();
    c.print();
    e.print();

    // A &&f = a; // error
    // const A &&g = a; // error

    // A &&h = a.get_lvalue(); // error
    // const A &&i = a.get_lvalue(); // error
    A &j = a.get_lvalue();
    const A &k = a.get_lvalue();
    cout << "--" << endl;

    j.print();
    k.print();

    A l;
    j = l;
    cout << "---" << endl;

    l.print();
    j.print();

    
    j = b;
    cout << "----" << endl;

    b.print();
    j.print();
    j = c;
    c.print();
    j.print();

    return 0;
}

输出结果为:  

construct: 0x7ffee6778878
copy construct: 0x7ffee6778878->0x7ffee6778868
copy construct: 0x7ffee6778878->0x7ffee6778858
copy construct: 0x7ffee6778878->0x7ffee6778848
-
[0x7ffee6778878,1]
[0x7ffee6778868,1]
[0x7ffee6778858,1]
[0x7ffee6778848,1]
--
[0x7ffee6778878,1]
[0x7ffee6778878,1]
construct: 0x7ffee6778830
copy assignment: 0x7ffee6778830->0x7ffee6778878
copy construct: 0x7ffee6778878->0x7ffee6778828
---
[0x7ffee6778830,2]
[0x7ffee6778878,2]
copy assignment: 0x7ffee6778868->0x7ffee6778878
copy construct: 0x7ffee6778878->0x7ffee6778820
----
[0x7ffee6778868,1]
[0x7ffee6778878,1]
copy assignment: 0x7ffee6778858->0x7ffee6778878
copy construct: 0x7ffee6778878->0x7ffee6778818
[0x7ffee6778858,1]
[0x7ffee6778878,1]

 

#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(int): " << this << endl;
    }
    A(const A &a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(const_cast<A*>(&a));
        cout << "copy construct: " << &a << "->" << this << endl;
    }
    A(A &&a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a);
        cout << "move construct: " << &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: " << &a << "->" << this << endl;
        return *this;
    }
    A operator=(A &&a) {
        *reinterpret_cast<char *>(this) = *reinterpret_cast<char *>(&a);
        cout << "move assignment: " << &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;
}

int main()
{
    test();
   cout << endl; test2();
return 0; }

输出结果为:

construct(int): 0x7ffee8a8b850
0x7ffee8a8b850
destruct: 0x7ffee8a8b850
construct(int): 0x7ffee8a8b850 0x7ffee8a8b850 construct(int): 0x7ffee8a8b838 move assignment: 0x7ffee8a8b838->0x7ffee8a8b850 copy construct: 0x7ffee8a8b850->0x7ffee8a8b840 destruct: 0x7ffee8a8b840 destruct: 0x7ffee8a8b838 0x7ffee8a8b850 destruct: 0x7ffee8a8b850

 拷贝了一次。

#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;
}

int main()
{
    test3();
    return 0;
}

输出结果为:

construct(-1): 0x7ffeeef72850
0x7ffeeef72850
construct(-3): 0x7ffeeef72838
construct(-2): 0x7ffeeef72828
enter move assignment
move assignment(-2): 0x7ffeeef72828->0x7ffeeef72850
copy construct(-2): 0x7ffeeef72850->0x7ffeeef72830
enter move assignment
move assignment(-3): 0x7ffeeef72838->0x7ffeeef72830
copy construct(-3): 0x7ffeeef72830->0x7ffeeef72840
destruct: 0x7ffeeef72840
destruct: 0x7ffeeef72830
destruct: 0x7ffeeef72828
destruct: 0x7ffeeef72838
0x7ffeeef72850
destruct: 0x7ffeeef72850

 

cpp test

标签:value   bsp   bit   highlight   cout   return   err   inter   pre   

原文地址:https://www.cnblogs.com/rongminglu/p/12989882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!