#include<iostream>
#include<string.h>
#include<vector>
/*
*C++ 11增加一个新的引用(reference)类型称作右值引用(R-value reference),
*标记为typename &&。他们能够以non-const值的方式传入,允许对象去改动他们
*而右值引用又是为了实现转移语义,避免内存的拷贝
*/
#define null 0
//! 左值
void PrintVal(int& i)
{
std::cout<<"Left ref:"<<i<<std::endl;
}
//! 右值
void PrintVal(int&& i)
{
std::cout<<"Right ref:"<<i<<std::endl;
}
//! 右值引用实现转移语义
class MyString
{
public:
MyString(const char* str)
{
if (str == null)
{
data_ = new char[1];
*data_ = '\0';
}
data_ = new char[strlen(str)+1];
strcpy(data_, str);
}
MyString(const MyString& myString)
{
std::cout<<"Left val ref:"<<myString.data_<<std::endl;
data_ = new char[strlen(myString.data_)];
strcpy(data_, myString.data_);
}
MyString(MyString&& myString)
{
std::cout<<"Right val ref:"<<myString.data_<<std::endl;
data_ = myString.data_;
myString.data_ = null;
}
const char* c_str()
{
return data_;
}
~MyString()
{
if (null != data_)
{
delete[] data_;
data_ = null;
}
}
private:
char* data_;
};
int main(int argc, char const *argv[])
{
int val = 6;
/* 左值右值引用函数重载 */
PrintVal(val);
PrintVal(5);
/* 通过右值引用实现转移语义 */
MyString str("str");
MyString str1(str); //! 左值
MyString str2(MyString("str2")); //! 应该是被编译器优化掉了
std::vector<MyString> v;
v.push_back(MyString("vec")); //! 这样不会被优化直接调用了右值引用函数,实现了转移语义
MyString str3(std::move(str)); //! 使用std::move强行右值
return 0;
}
运行结果:
Left ref:6
Right ref:5
Left val ref:str
Right val ref:vec
Right val ref:str
请按任意键继续. . .