设为首页 加入收藏

TOP

[C++]右值引用和转移语义(三)
2016-04-30 15:25:10 】 浏览:739
Tags:引用 转移 语义
d; // just use to test class test { private: int* array; int size; public: test() { array = NULL; size = 0; } test(int *arrays, int sizes) { array = new int [sizes]; for (int i = 0; i != sizes; i++) { array[i] = arrays[i]; } size = sizes; } test(test &orig) { cout << "L-value reference occur!" << endl; array = new int [orig.size]; while (size != orig.size) { array[size] = orig.array[size]; size++; } } test(test &&orig) { cout << "R-value reference occur!" << endl; array = orig.array; size = orig.size; orig.array = NULL; } test& operator = (const test &orig) { if (this != &orig) { cout << "L-value reference occur!" << endl; array = new int [orig.size]; while (size != orig.size) { array[size] = orig.array[size]; size++; } } return *this; } test& operator = (test &&orig) { if (this != &orig) { cout << "R-value reference occur!" << endl; array = orig.array; size = orig.size; orig.array = NULL; } return *this; } }; test check() { int a[4] = {1, 2, 3, 4}; test temp = test(a, 4); cout << &temp << endl; return temp; } // In this program, I try to explain how constructor works and how R-value reference works. int main() { // test 1 int a[4] = {1, 2, 3, 4}; cout << "test 1" << endl; test temp_test1(check()); cout << &temp_test1 << endl; // Through observe the address of the temporary pointer in func check() // and the address of temp_test1, we can see that they are the same object // It is clear that the IDE transfer the temporary object to global object in secret // without using r-value reference or copy constructor to save time. cout << endl; cout << "test 1 again" << endl; test temp = test(a, 4); test temp_test1_again(temp); // If we have created an object which is not temporary object, the IDE will use copy constructor. cout << endl; cout << "test 2" << endl; // Move function is used to transfer an object to R-value and obviously the IDE will use // r-value copy constructor. test temp_test2(move(test(a, 4))); cout << endl; cout << "test 2 again" << endl; test temp2 = test(a, 4); test temp_test2_again(move(temp2)); // Move function can also transfer a l-value object to a r-value object // which means that if we will never use temp2 again, we can let it be a r-value object // and give it to temp_test2_again, saving time without copy constructor. cout << endl; cout << "test 3" << endl; test temp_test3; test temp_test3_s(a, 4); temp_test3 = temp_test3_s; // just like the copy constructor. cout << endl; cout << "test 3 again" << endl; test temp_test3_again; test temp_test3_again_s(a, 4); temp_test3_again = move(temp_test3_again_s); cout << endl; cout << "test 4" << endl; test temp_test4; temp_test4 = test(a, 4); // If temp_test4 has been created and pass a temporary object to it, // the IDE will use r-value reference assignment operator. cout << endl; return 0; } // There is an another question we should know. // R-value reference doesn't mean we can transfer an address of one object to another object, // but we can transfer the "source" of one object to another object directly, // saving much for avoid using copy/assignment operation. // That is what I have mistaten before. //

本文大部分知识点来自C++11 标准新特性: 右值引用与转移语义。少许解释与理解属于我个人拙劣见解。

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【LeetCode】LeetCode――第15题.. 下一篇数据结构与算法――有向无环图的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目