Move semantics(C++11)
来源:互联网 发布:联想网络同传软件下载 编辑:程序博客网 时间:2024/06/06 00:04
/*
* Compile with:
* g++ move_test.c -o move_test -std=c++11 -g -fno-elide-constructors
* -fno-elide-constructors disabled the return value optimize.
*/
#include <iostream>
#include <utility>
class A {
public:
A(void)
{
a = new int;
std::cout << "Constructing(normal) A" << (void *)this << std::endl;
}
A(const class A &other)
{
std::cout << "Constructing(copy) A" << (void *)this << std::endl;
}
A(class A &&other)
{
std::cout << "Constructing(move) A" << (void *)this << std::endl;
a = other.a;
other.a = NULL;
}
~A(void)
{
std::cout << "Destructing A" << (void *)this << std::endl;
delete a;
}
void set(int i)
{
*a = i;
}
int get(void)
{
return *a;
}
private:
int *a;
};
class B {
private:
class A a;
public:
B(void)
{
std::cout << "Constructing(normal) B" << (void *)this << std::endl;
}
B(const class B &other)
: a(other.a)
{
std::cout << "Constructing(copy) B" << (void *)this << std::endl;
}
B(class B &&other)
:a(std::move(other.a))
{
std::cout << "Constructing(move) B" << (void *)this << std::endl;
}
~B(void)
{
std::cout << "Destructing B" << (void *)this << std::endl;
}
void set(int i)
{
a.set(i);
}
int get(void)
{
a.get();
}
};
class B func(void)
{
class B b;
b.set(23);
std::cout << "function Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return b;
}
int main(void)
{
class B b(func());
std::cout << b.get() << std::endl;
b.set('w');
std::cout << "Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return 0;
* Compile with:
* g++ move_test.c -o move_test -std=c++11 -g -fno-elide-constructors
* -fno-elide-constructors disabled the return value optimize.
*/
#include <iostream>
#include <utility>
class A {
public:
A(void)
{
a = new int;
std::cout << "Constructing(normal) A" << (void *)this << std::endl;
}
A(const class A &other)
{
std::cout << "Constructing(copy) A" << (void *)this << std::endl;
}
A(class A &&other)
{
std::cout << "Constructing(move) A" << (void *)this << std::endl;
a = other.a;
other.a = NULL;
}
~A(void)
{
std::cout << "Destructing A" << (void *)this << std::endl;
delete a;
}
void set(int i)
{
*a = i;
}
int get(void)
{
return *a;
}
private:
int *a;
};
class B {
private:
class A a;
public:
B(void)
{
std::cout << "Constructing(normal) B" << (void *)this << std::endl;
}
B(const class B &other)
: a(other.a)
{
std::cout << "Constructing(copy) B" << (void *)this << std::endl;
}
B(class B &&other)
:a(std::move(other.a))
{
std::cout << "Constructing(move) B" << (void *)this << std::endl;
}
~B(void)
{
std::cout << "Destructing B" << (void *)this << std::endl;
}
void set(int i)
{
a.set(i);
}
int get(void)
{
a.get();
}
};
class B func(void)
{
class B b;
b.set(23);
std::cout << "function Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return b;
}
int main(void)
{
class B b(func());
std::cout << b.get() << std::endl;
b.set('w');
std::cout << "Seperating..." << std::endl;
std::cout << b.get() << std::endl;
return 0;
}
Running results:
Constructing(normal) A0xbf965d1c
Constructing(normal) B0xbf965d1c
function Seperating...
23
Constructing(move) A0xbf965d4c
Constructing(move) B0xbf965d4c
Destructing B0xbf965d1c
Destructing A0xbf965d1c
Constructing(move) A0xbf965d48
Constructing(move) B0xbf965d48
Destructing B0xbf965d4c
Destructing A0xbf965d4c
23
Seperating...
119
Destructing B0xbf965d48
Destructing A0xbf965d48
0 0
- [C++11] Move Semantics
- Move semantics(C++11)
- C++11: Move Semantics (Move Constructor)
- What is move semantics?
- c++ move semantics
- Move semantics of RapidJSON
- Move semantics/theory - C++11, 26 of n
- C++11新特性之 Move semantics(移动语义)
- Move semantics and rvalue references in C++11
- rvalue references and Move semantics
- c++11 shared_ptr & unique_ptr & move semantics(右值引用) & emplace & lock_guard & final 和 override 关键字
- 返回局部变量如何避免拷贝:Move semantics and rvalue references in C++11
- Move.c && Move.h
- Move.h && Move.c
- C++0x尝鲜:移动语义(Move Semantics)
- Does returning a local object require move semantics?
- C++0x, rvalue reference, move semantics, RVO, NRVO — 我们到底要什么
- Effective Modern C++ 笔记 第五章:Rvalue References, Move Semantics, and Perfect Forwarding
- 让chrome支持本地Ajax请求
- nyoj58最少步数
- config servers are not in sync【mongo】
- mysql 导出指定列到文件
- GitHub客户端安装配置简介
- Move semantics(C++11)
- HD 1847 Good Luck in CET-4 Everybody!
- hdu 2089 不要62 数位dp
- main函数参数分析 处理命令行选项
- JDK7最新特性
- stat函数的使用 获取文件状态 可以将文件状态复制保存在一个stat结构地址中,以此取得文件的大小等状态
- android.view.InflateException:Error inflating class fragment
- Java 代理与动态代理
- UVALive 5964 LCM Extreme(数论 欧拉函数)