#include <iostream>
#include <memory>
using namespace std;

class B {
public: 
	virtual void Bar() { cout << "B::Bar()"<<endl; }
	virtual ~B() { cout << "B destroyed"<<endl; }
}; 
class BB:public B{
public: 
	void Bar() override { cout << "BB::Bar()"<<endl; }
	~BB() { cout << "BB destroyed"<<endl; }
};

class A
{
  private:
    unique_ptr<B> _b;
  public:
    template<class T>A(const T&b): _b(make_unique<T>(b)) { }

    void Foo()
    {
        _b->Bar();
    }
};

int main() {
	{
		B b; 
		A a{b}; 
		a.Foo();
	}
	{
		BB bb;
		A aa{bb};
		aa.Foo(); 
	}
	return 0;
}