I have a base class Base
and a derived class D
, and I'd like to have move constructor and move assignment operator automatically generated by the compiler for me. Following the Rule of Zero, I leave all memory management to the compiler and only use level-2 classes (no raw pointers, arrays, etc.):
#include <iostream>
class Base{
public:
Base(): a_(42) {}
void show() { std::cout << "Base " << a_ << std::endl; }
private:
int a_;
};
class D{
public:
D(): b_(666) {}
void show() { std::cout << "D " << b_ << std::endl; }
private:
int b_;
};
int main() {
Base b;
b.show();
D d;
d.show();
return 0;
}
This should be it, right?
Well, then I come across the C++ core guidelines:
A base class destructor should be either public and virtual, or protected and nonvirtual.
Ah, so I guess I'll have to add a destructor to Base
. But that'll do away with the automatically generated move functions!
What's the clean way out here?
Aucun commentaire:
Enregistrer un commentaire