The problem is: How can I do the deep clone for a unique_ptr which point to a base class, and the base class has some virtual functions.
The detail code:
class QueryRewrite {
public:
QueryRewrite(bool wasRes, ReturnMeta rmeta, const KillZone &kill_zone,
AbstractQueryExecutor *const executor)
: rmeta(rmeta), kill_zone(kill_zone),
executor(std::unique_ptr<AbstractQueryExecutor>(executor)) {}
QueryRewrite(const QueryRewrite &other_qr)
: rmeta(other_qr.rmeta), kill_zone(other_qr.kill_zone),
executor(other_qr.executor.get()) {}
QueryRewrite(){}
QueryRewrite(QueryRewrite &&other_qr) : rmeta(other_qr.rmeta),
executor(std::move(other_qr.executor)) {}
const ReturnMeta rmeta;
const KillZone kill_zone;
std::unique_ptr<AbstractQueryExecutor> executor;
};
In the copy constructor
QueryRewrite(const QueryRewrite &other_qr)
: rmeta(other_qr.rmeta), kill_zone(other_qr.kill_zone),
executor(other_qr.executor.get()) {}
executor(other_qr.executor.get())
is just a shallow copy to the executor, if I want to do the deep copy, how to change the code?
And executor
is a unique_ptr which point to an instance of AbstractQueryExecutor
, the AbstractQueryExecutor
is a base class which has some virtual functions, so if I change the code to
executor(new AbstractQueryExecutor(other_qr.executor.get()))
, it will say something like:
error: cannot allocate an object of abstract type ‘AbstractQueryExecutor’
note: because the following virtual functions are pure within ‘AbstractQueryExecutor’: