您需要了解的是,作为新手,编译器会生成大量您不知道的“隐式”代码。我们将使用您的代码class X
作为一个直接的例子:
class X {
int n;
public: //You didn't include this, but this won't work at all unless your constructors are public
X() {n = 0;}
X(int _n) {n = _n;}
};
在代码转换为对象代码之前,但在编译器获取类定义之后,它会将您的类转换为(大致)如下所示的内容:
class X {
int n;
public:
X() {n = 0;} //Default-Constructor
X(int _n) {n = _n;} //Other Constructor
//GENERATED BY COMPILER
X(X const& x) {n = x.n;} //Copy-Constructor
X(X && x) {n = x.n;} //Move-Constructor
X & operator=(X const& x) {n = x.n; return *this;} //Copy-Assignment
X & operator=(X && x) {n = x.n; return *this;} //Move-Assignment
~X() noexcept {} //Destructor
};
何时自动创建这些成员的规则并不是非常明显(这里有一个很好的入门参考 https://www.slideshare.net/ripplelabs/howard-hinnant-accu2014),但现在,您可以相信在这种情况下,确实会发生这种情况。
所以在你的main
函数,让我们回顾一下发生了什么,并通过注释来注意细节:
int main() {
X a; //Default-Constructor called
a = X(7);//Other Constructor called, then Move-Assignment operator called,
//then Destructor called on temporary created by `X(7)`
a = X(12); //Same as previous line
return 0;
//Destructor called on `a`
}
我们将添加更多行来显示这些调用的大多数(如果不是全部)各种排列:
int main() {
X a; //Default-Constructor
X b = a; //Copy-Constructor (uses copy-elision to avoid calling Default + copy-assign)
X c(5); //Other Constructor
X d{7}; //Also Other Constructor
X e(); //Declares a function! Probably not what you intended!
X f{}; //Default-Constructor
X g = X(8); //Other Constructor (uses copy-elision to avoid calling Other + move-assign + Destructor)
X h = std::move(b); //Move-Constructor (uses copy-elision to avoid calling Default + move-assign)
b = c; //Copy-assignment
b = std::move(d); //Move-assignment
d = X{15}; //Other Constructor, then Move-Assignment, then Destructor on `X{15}`.
//e = f; //Will not compile because `e` is a function declaration!
return 0;
//Destructor on `h`
//Destructor on `g`
//Destructor on `f`
//Destructor will NOT be called on `e` because `e` was a function declaration,
//not an object, and thus has nothing to clean up!
//Destructor on `d`
//Destructor on `c`
//Destructor on `b`
//Destructor on `a`
}
这应该涵盖基础知识。
最重要的是,编写上面这样的代码是否有可能导致内存泄漏?
正如所写,没有。然而,假设你的班级做了这样的事情:
class X {
int * ptr;
public:
X() {
ptr = new int{0};
}
};
现在,你的代码将会泄漏,因为每次X
创建后,您将拥有一个永远不会被删除的指针。
要解决这个问题,您需要确保 A) 析构函数正确清理指针,B) 您的复制/移动构造函数/运算符是正确的。
class X {
int * ptr;
public:
X() {
ptr = new int{0};
}
X(int val) {
ptr = new int{val};
}
X(X const& x) : X() {
*ptr = *(x.ptr);
}
X(X && x) : X() {
std::swap(ptr, x.ptr);
}
X & operator=(X const& x) {
*ptr = *(x.ptr);
return *this;
}
X & operator=(X && x) {
std::swap(ptr, x.ptr);
return *this;
}
~X() noexcept {
delete ptr;
}
};
如果按原样使用此代码,则不会泄漏内存main
功能或我的。但当然,如果你这样做,它并不能阻止泄漏:
int main() {
X * ptr = new X{};
return 0;
//Whelp.
}
一般来说,如果您根本不需要使用指针,建议您使用类似的东西std::unique_ptr
相反,因为它免费提供大部分内容。
int main() {
std::unique_ptr<X> ptr{new X{}};
return 0;
//Destructor called on *ptr
//`delete` called on ptr
}
在您的原始类中这是一个好主意,但需要注意的是,除非您明确更改它,否则您的类将不再可复制(尽管它仍然是可移动的):
class X {
std::unique_ptr<int> ptr;
public:
X() {
ptr.reset(new int{0});
}
X(int val) {
ptr.reset(new int{val});
}
//X(X && x); //auto generated by compiler
//X & operator=(X && x); //auto generated by compiler
//~X() noexcept; //auto generated by compiler
//X(X const& x); //Deleted by compiler
//X & operator=(X const& x); //Deleted by compiler
};
我们可以看到我之前版本的变化main
:
int main() {
X a; //Default-Constructor
//X b = a; //Was Copy-Constructor, no longer compiles
X c(5); //Other Constructor
X d{7}; //Also Other Constructor
X f{}; //Default-Constructor
X g = X(8); //Other Constructor (uses copy-elision to avoid calling Other + move-assign + Destructor)
X h = std::move(c); //Move-Constructor (uses copy-elision to avoid calling Default + move-assign)
//b = c; //Was Copy-assignment, no longer compiles
c = std::move(d); //Move-assignment
d = X{15}; //Other Constructor, then Move-Assignment, then Destructor on `X{15}`.
return 0;
//Destructor on `h`
//Destructor on `g`
//Destructor on `f`
//Destructor on `d`
//Destructor on `c`
//Destructor on `a`
}
如果你想使用std::unique_ptr
,但又希望生成的类是可复制的,您需要使用我讨论的技术自己实现复制构造函数。
这应该是关于它的!如果我错过了什么,请告诉我。