#include <iostream>
#include <memory>
struct Base
{
virtual void exec() { std::cout << "base" << std::endl; }
virtual ~Base() { }
};
struct Derived1 : public Base
{
void exec() override { std::cout << "derived1" <<std::endl; }
};
struct Derived2 : public Base
{
void exec() override { std::cout << "derived2" << std::endl; }
};
template<typename T> struct Wrapper : public T
{
bool b{true};
using T::T;
};
template<typename T> auto& asWrapper(T& mX)
{
return static_cast<Wrapper<T>&>(mX);
}
template<typename T, typename TWrapper> auto& asT(TWrapper& mX)
{
return reinterpret_cast<T&>(mX);
}
int main()
{
auto d1 = std::make_unique<Wrapper<Derived1>>();
auto d2 = std::make_unique<Wrapper<Derived2>>();
Base* bd1 = d1.get();
Base* bd2 = d2.get();
bd1->exec();
bd2->exec();
auto& bl1 = asWrapper(*bd1);
auto& bl2 = asWrapper(*bd2);
std::cout << bl1.b << " " << bl2.b << std::endl;
bl1.b = false;
std::cout << bl1.b << " " << bl2.b << std::endl;
bl2.b = false;
std::cout << bl1.b << " " << bl2.b << std::endl;
asT<Derived1>(bl1).exec();
asT<Derived2>(bl2).exec();
return 0;
}