cpp test

Posted rongminglu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cpp test相关的知识,希望对你有一定的参考价值。

#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

 

以上是关于cpp test的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 加载源图像固定用法(代码片段,不全)

Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段

c_cpp 快速代码片段,用于在统计(阻止)/ dev / rdsk中的设备时验证fstat64和stat64的行为。

c_cpp 这个简单的代码片段显示了如何使用有符号整数在C中完成插值。 for()循环确定要插入的范围

imgwarp.cpp:3143: error: (-215:Assertion failed) _src.total() > 0 in function ‘warpPerspective‘(代码片段

c_cpp UV Index Indicator订阅PubNub并使用颜色显示UV索引值。博文的代码片段。在这里查看项目:https:/