C++11 右值引用寻址
C++11 rvalue reference addressing
我试图理解 c++11 移动语义,并创建了以下沙箱项目:
main.cpp
#include "my-class.h"
#include <iostream>
MyOtherClass getRValue(void)
{
MyOtherClass retVal;
std::cout << "Address of getRValue local var is " << &retVal << std::endl;
std::cout << "Returning from getRValue...\n";
return retVal;
}
int main()
{
MyClass bar(getRValue());
std::cout << "Address of bar member is " << &(bar.getObjRef()) << std::endl;
return 0;
}
我的-class.h
#ifndef MY_CLASS_H_INCLUDED
#define MY_CLASS_H_INCLUDED
#include <iostream>
class MyOtherClass
{
private:
int somePOD;
char someOtherPOD;
public:
MyOtherClass(void);
MyOtherClass(MyOtherClass const& argOther);
MyOtherClass(MyOtherClass&& argOther);
};
class MyClass
{
private:
MyOtherClass obj;
public:
MyClass(MyOtherClass&& arg);
MyOtherClass& getObjRef(void);
};
#endif // MY_CLASS_H_INCLUDED
我的-class.cpp
#include "my-class.h"
// Class MyOtherClass
MyOtherClass::MyOtherClass(void)
: somePOD(42), someOtherPOD('x')
{
std::cout << "MyOtherClass c'tor called.\n";
}
MyOtherClass::MyOtherClass(MyOtherClass const& argOther)
{
std::cout << "MyOtherClass copy c'tor called.\n";
}
MyOtherClass::MyOtherClass(MyOtherClass&& argOther)
{
std::cout << "MyOtherClass move c'tor called.\n";
}
// Class MyClass
MyClass::MyClass(MyOtherClass&& arg)
: obj(std::move(arg))
{
std::cout << "MyClass c'tor called.\n";
}
MyOtherClass& MyClass::getObjRef(void)
{
return this->obj;
}
此代码打印输出:
MyOtherClass c'tor called.
Address of getRValue local var is 0x7fff882a2db8
Returning from getRValue...
MyOtherClass move c'tor called.
MyClass c'tor called.
Address of bar member is 0x7fff882a2db0
我的问题是:为什么本地变量的地址是。和酒吧会员不同?这两个是相同的,这不是移动语义的全部意义吗?或者我只是在我的示例中做错了什么?
每个对象都有自己的地址。由于 bar
和 retVal
是不同的对象,因此它们具有不同的地址。移动不会改变对象的地址。
移动的作用是允许您将对象的内脏从一个对象移动到另一个对象。根据 class 的制作方式,这可以极大地提升性能。例如,如果我们有一个类似于 std::vector
的 class,它将在其中有一个指向它分配的存储空间的指针。移动允许您仅将指针和大小从移动对象复制到移动对象。然后你只需将 moved from object 中的指针设置为空指针并将大小设置为 0。现在我们不必复制任何元素或分配任何存储,而如果我们正在制作副本则我们必须这样做.
我试图理解 c++11 移动语义,并创建了以下沙箱项目:
main.cpp
#include "my-class.h"
#include <iostream>
MyOtherClass getRValue(void)
{
MyOtherClass retVal;
std::cout << "Address of getRValue local var is " << &retVal << std::endl;
std::cout << "Returning from getRValue...\n";
return retVal;
}
int main()
{
MyClass bar(getRValue());
std::cout << "Address of bar member is " << &(bar.getObjRef()) << std::endl;
return 0;
}
我的-class.h
#ifndef MY_CLASS_H_INCLUDED
#define MY_CLASS_H_INCLUDED
#include <iostream>
class MyOtherClass
{
private:
int somePOD;
char someOtherPOD;
public:
MyOtherClass(void);
MyOtherClass(MyOtherClass const& argOther);
MyOtherClass(MyOtherClass&& argOther);
};
class MyClass
{
private:
MyOtherClass obj;
public:
MyClass(MyOtherClass&& arg);
MyOtherClass& getObjRef(void);
};
#endif // MY_CLASS_H_INCLUDED
我的-class.cpp
#include "my-class.h"
// Class MyOtherClass
MyOtherClass::MyOtherClass(void)
: somePOD(42), someOtherPOD('x')
{
std::cout << "MyOtherClass c'tor called.\n";
}
MyOtherClass::MyOtherClass(MyOtherClass const& argOther)
{
std::cout << "MyOtherClass copy c'tor called.\n";
}
MyOtherClass::MyOtherClass(MyOtherClass&& argOther)
{
std::cout << "MyOtherClass move c'tor called.\n";
}
// Class MyClass
MyClass::MyClass(MyOtherClass&& arg)
: obj(std::move(arg))
{
std::cout << "MyClass c'tor called.\n";
}
MyOtherClass& MyClass::getObjRef(void)
{
return this->obj;
}
此代码打印输出:
MyOtherClass c'tor called.
Address of getRValue local var is 0x7fff882a2db8
Returning from getRValue...
MyOtherClass move c'tor called.
MyClass c'tor called.
Address of bar member is 0x7fff882a2db0
我的问题是:为什么本地变量的地址是。和酒吧会员不同?这两个是相同的,这不是移动语义的全部意义吗?或者我只是在我的示例中做错了什么?
每个对象都有自己的地址。由于 bar
和 retVal
是不同的对象,因此它们具有不同的地址。移动不会改变对象的地址。
移动的作用是允许您将对象的内脏从一个对象移动到另一个对象。根据 class 的制作方式,这可以极大地提升性能。例如,如果我们有一个类似于 std::vector
的 class,它将在其中有一个指向它分配的存储空间的指针。移动允许您仅将指针和大小从移动对象复制到移动对象。然后你只需将 moved from object 中的指针设置为空指针并将大小设置为 0。现在我们不必复制任何元素或分配任何存储,而如果我们正在制作副本则我们必须这样做.