我用TDM-GCC 4.6.1编译器对rvalue引用做了一些实验,并做了一些我无法用理论解释的有趣观察.我想那里的专家帮我解释一下.
我有一个非常简单的程序,不处理对象,但int原语,并定义了2个函数:foo1(通过rvalue引用返回局部变量)和foo2(按值返回局部变量)
#include <iostream>
using namespace std;
int &&foo1();
int foo2();
int main()
{
int&& variable1 = foo1();
//cout << "My name is softwarelover." << endl;
cout << "variable1 is: " << variable1 << endl; // Prints 5.
cout << "variable1 is: " << variable1 << endl; // Prints 0.
int&& variable2 = foo2();
cout << "variable2 is: " << variable2 << endl; // Prints 5.
cout << "variable2 is still: " << variable2 << endl; // Still prints …Run Code Online (Sandbox Code Playgroud) 我正在看这个显示移动构造函数的答案:
#include <cstring>
#include <algorithm>
class string
{
char* data;
public:
string(const char* p)
{
size_t size = strlen(p) + 1;
data = new char[size];
memcpy(data, p, size);
}
~string()
{
delete[] data;
}
string(const string& that)
{
size_t size = strlen(that.data) + 1;
data = new char[size];
memcpy(data, that.data, size);
}
};
Run Code Online (Sandbox Code Playgroud)
然后介绍一个移动构造函数:
string(string&& that) // string&& is an rvalue reference to a string
{
data = that.data;
that.data = 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我们分配指向data该值的指针that.data,肯定这会导致内存泄漏,该new …
对象有一个字符串,需要构造.
#include <string>
class SDLException
{
private:
std::string what_str;
public:
SDLException(const std::string &msg);
~SDLException(void);
};
Run Code Online (Sandbox Code Playgroud)
该字符串具有隐藏的依赖关系,我需要考虑(SDL_GetError()).我可以在函数中构造字符串.但我不知道如何使用该函数的返回值来初始化字符串成员.
#include "SDLException.hpp"
#include <sstream>
#include <string>
#include <SDL.h>
static void buildSTR(const std::string &msg)
{
std::ostringstream stream;
stream << msg << " error: " << SDL_GetError();
std::string str = stream.str();
//if i return a string here it would be out of scope when i use it
}
SDLException::SDLException(const std::string &msg)
: what_str(/*i want to initialise this string here*/)
{}
SDLException::~SDLException(void){}
Run Code Online (Sandbox Code Playgroud)
如何what_str以最小的开销初始化成员?内容 …
在阅读有关std :: move语义和rvalue引用的一些文章时,我发现它们是因为复制数据是一项昂贵的工作而引入的.真的!复制很昂贵,但这与我们在C++中使用"REFERENCES"的原因不同.它们也只是帮助传递变量的地址并防止昂贵的复制过程.
那么为什么要引入std :: move和rvalue呢?我的意思是什么工作不能正常引用做前者可以做的?
我的问题主要涉及功能.我的意思是当你将一个变量传递给一个函数时,哪个更好?为什么?: - 1.通过左值参考2.使用移动通过右值参考
所以我有一个函数需要将std :: vector作为参数.我想知道声明参数的最佳方法,以便底层数组不会被深度复制,因为它可能相当大.
// which should I choose?
void myFunc(std::vector<char>); // 1
void myFunc(std::vector<char>&); // 2
void myFunc(std::vector<char>&&); // 3
void myFunc(std::vector<char>*) // 4
Run Code Online (Sandbox Code Playgroud)
我该选哪个?另外,我不会修改函数中的向量,所以不应该添加const?我应该重载该功能并将它们组合在一起吗?
在查看一些代码时,我遇到了一个带有以下参数的方法
methodName(const std::string & parameterName)
Run Code Online (Sandbox Code Playgroud)
我知道const用于一个在方法中不能改变的值,而&是一个引用的值.
为什么以及何时应该使用此而不仅仅是一个const字符串?
我正在阅读关于C++ 11中的移动语义,现在我正在尝试理解移动构造函数的实现.假设我们有以下类:
struct A {
A(){ }
virtual ~A(){ }
A(const A&&){ }
};
struct B{
int i;
A a;
virtual ~B(){ }
B(const B&& b){
i = b.i;
i = 0;
//How to move a??
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是如何A在B一个体内调用移动构造函数?我会用std::swap,但寻找它我发现了一些描述.参数是左值引用类型,因此它与移动语义无关.该怎么办?
我举以下例子来说明我的问题:
// Example program
#include <iostream>
#include <string>
class Abc
{
private:
int a_;
public:
Abc(int a):a_(a) {};
Abc& operator = ( const Abc &obj);
Abc(const Abc &obj);
};
Abc& Abc::operator = ( const Abc &obj)
{
a_ = obj.a_;
return *this;
}
Abc::Abc(const Abc &obj)
{
a_ = obj.a_;
}
int main()
{
Abc obj1(3);
Abc obj2=obj1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当Abc obj2=obj1被调用时,我期望 Abc& Abc::operator = ( const Abc &obj)会被调用而实际上Abc::Abc(const Abc &obj)被调用。我对此感到困惑。最重要的是,由于它a_是一个私有成员变量,因此在a_=obj.a_ …
我需要一个char*我的文件名.它应该是这样的:cards/h11.bmp
我有一个函数,我从各种SO文章拼凑而成:
char* getFileName(int* pc1_no, char* suite)
{
int number;
char pCard1[80];
strcpy_s(pCard1, "cards/");
strcat_s(pCard1, suite);
number = *pc1_no;
cout << number << endl;
string str = to_string(number);
char const *pchar = str.c_str();
strcat_s(pCard1, pchar);
strcat_s(pCard1, ".bmp");
return pCard1;
}
Run Code Online (Sandbox Code Playgroud)
当然,返回垃圾.我没有得到指针值.我很确定我用指针犯了一个愚蠢的错误.提前致谢.
我真的很困惑,我已经看了几次,它仍然没有点击。就内存使用而言,复制构造函数和移动构造函数在幕后是什么样子的?我真的不明白移动构造函数中的“窃取资源”是什么。移动构造函数应该用于动态分配的内存还是堆栈上的内存?
我还被告知,如果我有这样的代码:
void someFunction(Obj obj){
//code
cout << &obj << endl;
}
int main(){
Obj o;
cout << &o << endl;
someFunction(o);
}
Run Code Online (Sandbox Code Playgroud)
这o将被复制到obj. 我的印象是复制会在内存中创建一个新项目并将传递的数据复制到该对象的内存地址。因此obj会在内存中创建一个新空间,并将o的数据复制到其中。但是,我得到了与oand相同的确切地址obj,所以基本上我不知道发生了什么。