栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++左右值(引用)示例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++左右值(引用)示例

左右值(引用)示例
  • 在此处,请思考左右值相关的内容
  • forward和move的作用
  • namespace的使用
  • 重载赋值运算符的操作
#include
using namespace std;

#define func(x) __func(x, "func(" #x ")")
#define func2(x) __func2(x, "func2(" #x ")")

void __func2(int &x, const char *str) {
    cout << str << " is left value" << endl;
    return ;
}

void __func2(int &&x, const char *str) {
    cout << str << " is right value" << endl;
    return ;
}

void __func2(double &&x, const char *str) {
    cout << str << " is double value" << endl;
    return ;
}

void __func(int &x, const char *str) {
    cout << str << " is left value" << endl;
    func2(x);
    return ;
}

void __func(int &&x, const char *str) {
    cout << str << " is right value" << endl;
    func2(forward(x));
    return ;
}

namespace haizei {
    class vector {
    public:
        vector(int n = 10) : __size(n), data(new int[n]){
            cout << "default constructor" << endl;
        }
        vector(const vector &v) : __size(v.size()), data(new int[__size]) {
            cout << "deep copy constructor" << endl;
            for (int i = 0; i < size(); i++) data[i] = v[i];
            return ;
        }
        vector(vector &&v) : __size(v.size()), data(v.data) {
            cout << "move copy constructor" << endl;
            v.data = nullptr;
            v.__size = 0;
        }
        vector operator+(const vector &v) {
            vector ret(v.size() + this->size());
            vector &now = *this;
            for (int i = 0; i < size(); i++) {
                ret[i] = now[i];
            }
            for (int i = size(); i < ret.size(); i++) {
                ret[i] = now[i - size()];
            }
            return ret;
        }
        int &operator[](int ind) const {
            return this->data[ind];
        }
        int size() const {return __size; }
        ~vector() {
            if (data) delete[] data;
            data = nullptr;
            __size = 0;
        }
    private:
        int __size, *data;
    };

} //end of haizei

ostream &operator<<(ostream &out, const haizei::vector &v) {
    for (int i = 0; i < v.size(); i++) {
        out << v[i] << " ";
    }
    return out;
}

int main() {
    haizei::vector v1, v2;
    for (int i = 0; i < v1.size(); i++) v1[i] = rand() % 100;
    for (int i = 0; i < v2.size(); i++) v2[i] = rand() % 100;
    haizei::vector v3(v1 + v2);
    cout << v1 << endl;
    cout << v2 << endl;
    cout << v3 << endl;
    haizei::vector v4(move(v1));
    cout << v1 << endl;
    cout << v4 << endl;
    //return 0;
    int x = 1234,y = 456;
    func(1234);
    func(x);
    func(x + y);
    func(x++);
    func(++x);
    func(x + 123);
    func(y *= 2);
    func(y += 2);
    func(y * 3);
    return 0;
}


运行结果 不关闭编译器优化
default constructor
default constructor
default constructor
83 86 77 15 93 35 86 92 49 21 
62 27 90 59 63 26 40 26 72 36 
83 86 77 15 93 35 86 92 49 21 83 86 77 15 93 35 86 92 49 21 
move copy constructor

83 86 77 15 93 35 86 92 49 21 
func(1234) is right value
func2(forward(x)) is double value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(forward(x)) is double value
func(x++) is right value
func2(forward(x)) is double value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(forward(x)) is double value
func(y *= 2) is left value
func2(x) is left value
func(y += 2) is left value
func2(x) is left value
func(y * 3) is right value
func2(forward(x)) is double value

关掉编译器优化-fno-elide-constructors
default constructor
default constructor
default constructor
move copy constructor
move copy constructor
83 86 77 15 93 35 86 92 49 21 
62 27 90 59 63 26 40 26 72 36 
83 86 77 15 93 35 86 92 49 21 83 86 77 15 93 35 86 92 49 21 
move copy constructor

83 86 77 15 93 35 86 92 49 21 
func(1234) is right value
func2(forward(x)) is double value
func(x) is left value
func2(x) is left value
func(x + y) is right value
func2(forward(x)) is double value
func(x++) is right value
func2(forward(x)) is double value
func(++x) is left value
func2(x) is left value
func(x + 123) is right value
func2(forward(x)) is double value
func(y *= 2) is left value
func2(x) is left value
func(y += 2) is left value
func2(x) is left value
func(y * 3) is right value
func2(forward(x)) is double value

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444143.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号