I've learnt that when you use pointers in a class, you should implement the rule of 5. If you do not use pointers then you're okay, and in fact its preferable, to use the defaults. However, how does this work with smart pointers? For example a class containing an int*
might look like this:
class A {
private:
int *num_;
public:
explicit A(int* num) : num_(num) {}
~A() {
delete num_;
}
A(const A &other) {
if (this != &other) {
num_ = other.num_;
}
}
A(A &&other) noexcept {
if (this != &other) {
num_ = other.num_;
}
}
A &operator=(A &other) {
if (this == &other) {
this->num_ = other.num_;
}
return *this;
}
A &operator=(A &&other) noexcept {
if (this == &other) {
this->num_ = other.num_;
}
return *this;
};
};
But if we use smart pointers, is it sufficient to just do this?
class B {
private:
std::unique_ptr<int> num_;
public:
explicit B(int num) : num_(std::make_unique<int>(num)) {};
};
Yes that is sufficent. The unique pointer does manage the memory. However, your two classes will behave differently because std::unique_ptr
cannot be copied, hence there will be no compiler generated copy constructor nor assignment for B
.
Also note that you implemented all methods for the rule of 5, but not correctly. As mentioned in a comment, copying an A
will result in two instances having the same pointer and delete it upon destruction. Actually getting this right is the whole point about the rule of 3/5 and why you should prefer the rule of 0.