Is the following a valid design in C++?
#include <iostream>
#include <memory>
using namespace std;
struct Base {
virtual void SomeMethod() {
std::cout<<"Do Something"<<std::endl;
}
};
struct Der : public Base {
virtual void SomeMethod() override = 0;
};
struct DerDer : Der {
virtual void SomeMethod() override
{
std::cout<<"Do something derived."<<std::endl;
}
};
int main(int argc, char *argv[])
{
auto x = std::unique_ptr<Base>(new DerDer);
x->SomeMethod();
return 0;
}
It works on g++ 6.2.
I'm deriving from a normal class, converting that method to abstract, and then overriding it after deriving again.
Is it correct in C++ to derive from a class, and then turn an implemented method back to abstract?
The reason I need to do this, is that in a big program, to which I'm adding features, I'm trying to avoid a diamond model. To do that, I'm planning to put all common methods in the class Der
, and make everything inherit from it, without messing with the Base
class.