My goal is to take ownership of the temporary object created with Derived()
in main()
with BaseHolder
.
I'm getting an error at m_bases.push_back(base)
because it invokes Base::Base()
which is illegal because it's a purely virtual class, but I don't understand why it's invoked in the first place because my thinking is that the already constructed object would just be moved into m_bases
.
Is there a way to do what I'm trying to do here?
#include <vector>
using namespace std;
class Base {
public:
virtual void foo() = 0;
};
class BaseHolder {
vector<Base> m_bases;
public:
void add(Base&& base) {
m_bases.push_back(base);
}
};
class Derived : public Base {
public:
void foo() override {}
};
int main() {
BaseHolder base_holder;
base_holder.add(Derived());
return 0;
}