Я хочу сделать GroupBase интерфейсом...
Код: Выделить всё
#include "string"
#include "unordered_map"
template
class BaseV {
public:
BaseV() {
//logic
}
virtual auto run() -> bool { return false; }
protected:
//members
};
class GroupBase {
public:
GroupBase() {
//logic
}
template
auto run(const std::string &name, const Vty &value) -> void {
createValue(name);
((BaseV *)_values.at(name))->run();
}
template
auto createValue(const std::string &name) -> void {
static_assert(false, "You must override createValue method to instead GroupBase::createValue!");
}
protected:
std::unordered_map _values{};
};
template
class ThirdPartyClass{};
template
class DerivedV : public BaseV {
public:
DerivedV() : BaseV() {
_data = new ThirdPartyClass();
}
auto run() -> bool override {
//call function in _data;
}
protected:
ThirdPartyClass* _data;
};
class GroupDerived : public GroupBase {
public:
GroupDerived () : GroupBase() {
//logic
}
template
auto createValue(const std::string &name) -> void {
_values.emplace(name, new DerivedV());
}
};
int main(int argc, char** argv) {
auto group = GroupDerived();
group.run("a", 1);
}
Код: Выделить всё
static_assert
Если удалить эту строку:
Код: Выделить всё
static_assert(false, "You must override createValue method to instead GroupBase::createValue!");
So how to make it more reasonable?
(I have to add a new ThirdPartyClass 1, 2, 3...at any time...)
Источник: https://stackoverflow.com/questions/781 ... n-subclass