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(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的主要内容,如果未能解决你的问题,请参考以下文章

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:/