空的用户定义的移动构造函数
Empty user-defined move constructor
我为理解移动 CTOR 行为而编写的以下代码片段让我很难理解它的输出:
#include <iostream>
class Temp
{
public:
Temp(){
std::cout << "Temp DEFAULT CTOR called" << std::endl;
mp_Val = nullptr;
}
Temp(int inp) {
std::cout << "Temp CTOR called" << std::endl;
mp_Val = new int(inp);
}
Temp(const Temp& inp) {
std::cout << "Temp COPY CTOR called" << std::endl;
mp_Val = new int(*inp.mp_Val);
}
Temp& operator= (const Temp& inp) {
std::cout << "Temp ASSIGNMENT OPER called" << std::endl;
mp_Val = new int(*inp.mp_Val);
return *this;
}
int* mp_Val;
};
class B
{
public:
B(){
std::cout << "Class B DEFAULT CTOR" << std::endl;
mp_Val = nullptr;
}
B(int inp) {
std::cout << "Class B CTOR" << std::endl;
mp_Val = new Temp(inp);
}
B(const B& in) {
std::cout << "Class B COPY CTOR" << std::endl;
mp_Val = in.mp_Val;
}
B(B&& in){
std::cout << "Class B MOVE CTOR" << std::endl; //Doubt: 1
}
Temp *mp_Val;
};
int main() {
B obj1(200);
B obj2 = std::move(obj1);
auto temp = obj1.mp_Val;
std::cout << "Obj1 B::Temp address: " << obj1.mp_Val << std::endl;
std::cout << "Obj2 B::Temp address: " << obj2.mp_Val << std::endl; //Doubt: 2
return 0;
}
输出:
Class B CTOR
Temp CTOR called
Class B MOVE CTOR
Obj1 B::Temp address: 0xd48030
Obj2 B::Temp address: 0x400880
GCC 版本:4.6.3
我的问题是关于标记为Doubt 2的行。地址不应该打印为0吗?根据我的理解,由于我在 class B
中定义了一个空移动 CTOR(标记为 Doubt 1),它应该调用 class Temp
的默认 CTOR(它没有调用明显从日志中)初始化其成员变量 mp_Val
Temp
.
显然我缺少一些东西。
As per my understanding, as I have defined an empty move CTOR (marked as Doubt 1) in class B
, it should call the default CTOR of class Temp
(which it's not calling as evident from the logs) to initialise its member variable mp_Val
of type Temp
.
您的成员变量不是Temp
类型,而是Temp *
类型。你是对的,缺少初始化器意味着该成员将被默认构造,并且对于 Temp
类型,这将涉及调用默认构造函数。但是,对于指针类型,默认构造使对象未初始化。
我为理解移动 CTOR 行为而编写的以下代码片段让我很难理解它的输出:
#include <iostream>
class Temp
{
public:
Temp(){
std::cout << "Temp DEFAULT CTOR called" << std::endl;
mp_Val = nullptr;
}
Temp(int inp) {
std::cout << "Temp CTOR called" << std::endl;
mp_Val = new int(inp);
}
Temp(const Temp& inp) {
std::cout << "Temp COPY CTOR called" << std::endl;
mp_Val = new int(*inp.mp_Val);
}
Temp& operator= (const Temp& inp) {
std::cout << "Temp ASSIGNMENT OPER called" << std::endl;
mp_Val = new int(*inp.mp_Val);
return *this;
}
int* mp_Val;
};
class B
{
public:
B(){
std::cout << "Class B DEFAULT CTOR" << std::endl;
mp_Val = nullptr;
}
B(int inp) {
std::cout << "Class B CTOR" << std::endl;
mp_Val = new Temp(inp);
}
B(const B& in) {
std::cout << "Class B COPY CTOR" << std::endl;
mp_Val = in.mp_Val;
}
B(B&& in){
std::cout << "Class B MOVE CTOR" << std::endl; //Doubt: 1
}
Temp *mp_Val;
};
int main() {
B obj1(200);
B obj2 = std::move(obj1);
auto temp = obj1.mp_Val;
std::cout << "Obj1 B::Temp address: " << obj1.mp_Val << std::endl;
std::cout << "Obj2 B::Temp address: " << obj2.mp_Val << std::endl; //Doubt: 2
return 0;
}
输出:
Class B CTOR
Temp CTOR called
Class B MOVE CTOR
Obj1 B::Temp address: 0xd48030
Obj2 B::Temp address: 0x400880
GCC 版本:4.6.3
我的问题是关于标记为Doubt 2的行。地址不应该打印为0吗?根据我的理解,由于我在 class B
中定义了一个空移动 CTOR(标记为 Doubt 1),它应该调用 class Temp
的默认 CTOR(它没有调用明显从日志中)初始化其成员变量 mp_Val
Temp
.
显然我缺少一些东西。
As per my understanding, as I have defined an empty move CTOR (marked as Doubt 1) in
class B
, it should call the default CTOR of classTemp
(which it's not calling as evident from the logs) to initialise its member variablemp_Val
of typeTemp
.
您的成员变量不是Temp
类型,而是Temp *
类型。你是对的,缺少初始化器意味着该成员将被默认构造,并且对于 Temp
类型,这将涉及调用默认构造函数。但是,对于指针类型,默认构造使对象未初始化。