未调用移动构造函数
Move Constructor is not getting called
我编写了以下代码来演示移动构造。但是即使我使用了 std::move(),移动构造函数也没有被调用。有人可以看看这里有什么问题吗?
#include "point.hpp"
using namespace std;
class point;
d2point add_point(d2point&& p1, d2point&& p2)
{
cout << "inside add point function" << endl;
d2point z;
z.setX(p1.getX() + p2.getX());
z.setY(p1.getY() + p2.getY());
return z;
}
int main()
{
d2point x(2,3);
d2point y(2,3);
d2point z = add_point(move(x), move(y));
z.print_point();
x.print_point();
return 0;
}
以下代码来自point.hpp
using namespace std;
class point
{
private:
int x;
public:
point(){cout << "point default constructor gets called" << endl;}
point(int x){ cout << "point param constructor gets called" << endl;
this->x = x;
}
point(const point& p){
cout << "point copy constructor gets called" << endl;
this->x = p.x;
}
point(point&& other):x(std::move(other.x)) {
cout << "point move constructor gets called" << endl;
}
int getX(){return x;}
void setX(int x) { this->x = x; }
virtual void print_point() {}
virtual ~point(){cout << "point destructor gets called" << endl;}
};
class d2point: public point
{
private:
int y;
public:
d2point()
{
cout << "d2point default constructor gets called" << endl;
}
d2point(int x, int y):point(x) {
cout << "d2point parameterized constructor gets called" << endl;
this->y = y;
}
d2point(const d2point& rhs): point(rhs) {
cout << "d2point copy constructor gets called" << endl;
this->y = rhs.y;
}
d2point(d2point&& rhs):point(std::move(rhs)) {
cout << "d2point move constructor gets called" << endl;
this->y = std::move(rhs.y);
rhs.y = 0;
}
int getY(){return y;}
void setY(int y) { this->y = y; }
void print_point(){
cout << "(" << getX() << "," << y << ")" << endl;
}
~d2point(){ cout << "d2point destructor gets called" << endl;}
};
程序输出如下:
point param constructor gets called
d2point parameterized constructor gets called
point param constructor gets called
d2point parameterized constructor gets called
inside add point function
point default constructor gets called
d2point default constructor gets called
(4,6)
(2,3)
d2point destructor gets called
point destructor gets called
d2point destructor gets called
point destructor gets called
d2point destructor gets called
point destructor gets called
这里正在调用采用右值引用的函数,但是移动构造函数语句没有被打印出来,它确实被调用了,或者正在调用其他一些成员函数。请看这里的问题。
除非您从 d2point&&
构造 d2point
的新实例,否则不会调用移动构造函数。在您的 add_point
函数中,您将 rvalue references 转为 d2point
:这意味着您没有构建任何新实例,但仅引用现有实例:
d2point add_point(d2point&& p1, d2point&& p2)
// ^^ ^^
请记住 std::move
只是对 右值引用的强制转换 。
d2point z = add_point(move(x), move(y));
在上面的行中,您将 x
和 y
都转换为 d2point&&
- 它们绑定到 p1
和 p2
。尚未进行施工。最后,将 add_point
调用的结果存储到 z
中。由于return值优化,您极有可能也看不到在那里调用的移动构造函数。
正在将 add_point
更改为
d2point add_point(d2point p1, d2point p2)
肯定会调用d2point::d2point(d2point&&)
.
我编写了以下代码来演示移动构造。但是即使我使用了 std::move(),移动构造函数也没有被调用。有人可以看看这里有什么问题吗?
#include "point.hpp"
using namespace std;
class point;
d2point add_point(d2point&& p1, d2point&& p2)
{
cout << "inside add point function" << endl;
d2point z;
z.setX(p1.getX() + p2.getX());
z.setY(p1.getY() + p2.getY());
return z;
}
int main()
{
d2point x(2,3);
d2point y(2,3);
d2point z = add_point(move(x), move(y));
z.print_point();
x.print_point();
return 0;
}
以下代码来自point.hpp
using namespace std;
class point
{
private:
int x;
public:
point(){cout << "point default constructor gets called" << endl;}
point(int x){ cout << "point param constructor gets called" << endl;
this->x = x;
}
point(const point& p){
cout << "point copy constructor gets called" << endl;
this->x = p.x;
}
point(point&& other):x(std::move(other.x)) {
cout << "point move constructor gets called" << endl;
}
int getX(){return x;}
void setX(int x) { this->x = x; }
virtual void print_point() {}
virtual ~point(){cout << "point destructor gets called" << endl;}
};
class d2point: public point
{
private:
int y;
public:
d2point()
{
cout << "d2point default constructor gets called" << endl;
}
d2point(int x, int y):point(x) {
cout << "d2point parameterized constructor gets called" << endl;
this->y = y;
}
d2point(const d2point& rhs): point(rhs) {
cout << "d2point copy constructor gets called" << endl;
this->y = rhs.y;
}
d2point(d2point&& rhs):point(std::move(rhs)) {
cout << "d2point move constructor gets called" << endl;
this->y = std::move(rhs.y);
rhs.y = 0;
}
int getY(){return y;}
void setY(int y) { this->y = y; }
void print_point(){
cout << "(" << getX() << "," << y << ")" << endl;
}
~d2point(){ cout << "d2point destructor gets called" << endl;}
};
程序输出如下:
point param constructor gets called
d2point parameterized constructor gets called
point param constructor gets called
d2point parameterized constructor gets called
inside add point function
point default constructor gets called
d2point default constructor gets called
(4,6)
(2,3)
d2point destructor gets called
point destructor gets called
d2point destructor gets called
point destructor gets called
d2point destructor gets called
point destructor gets called
这里正在调用采用右值引用的函数,但是移动构造函数语句没有被打印出来,它确实被调用了,或者正在调用其他一些成员函数。请看这里的问题。
除非您从 d2point&&
构造 d2point
的新实例,否则不会调用移动构造函数。在您的 add_point
函数中,您将 rvalue references 转为 d2point
:这意味着您没有构建任何新实例,但仅引用现有实例:
d2point add_point(d2point&& p1, d2point&& p2)
// ^^ ^^
请记住 std::move
只是对 右值引用的强制转换 。
d2point z = add_point(move(x), move(y));
在上面的行中,您将 x
和 y
都转换为 d2point&&
- 它们绑定到 p1
和 p2
。尚未进行施工。最后,将 add_point
调用的结果存储到 z
中。由于return值优化,您极有可能也看不到在那里调用的移动构造函数。
正在将 add_point
更改为
d2point add_point(d2point p1, d2point p2)
肯定会调用d2point::d2point(d2point&&)
.