문제는 다음과 같습니다. 기본 클래스를 가리키는 unique_ptr에 대해 딥 클론을 수행하고 기본 클래스에 몇 가지 가상 함수가 있습니다.기본 클래스를 가리키는 unique_ptr의 딥 복제
세부 코드 : 나는 어떻게 코드를 변경하는 깊은 복사본을 수행하려는 경우 복사 생성자
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())
에서
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;
};
는 집행 단지 얕은 사본입니다 ?
그리고 executor
는 AbstractQueryExecutor
일부 가상 기능이 기본 클래스는 AbstractQueryExecutor
의 인스턴스에 A 지점 unique_ptr, 그래서 내가 executor(new AbstractQueryExecutor(other_qr.executor.get()))
에 코드를 변경하는 경우, 그것은 말할 것이다 뭔가 같은 :
error: cannot allocate an object of abstract type ‘AbstractQueryExecutor’
note: because the following virtual functions are pure within ‘AbstractQueryExecutor’:
C는'unique_ptr'과 그것들을 작동시키는 데 필요한 모든 기초를 도입했습니다! 시원한! – StoryTeller
http://stackoverflow.com/questions/16030081/copy-constructor-for-a-class-with-unique-ptr – Rishi
'unique_ptr'은 실제로 문제와 관련이 없습니다. 이것은 일반적으로 가상'clone()'멤버 함수로 해결되는 다형성 복사의 고전적인 문제입니다. – Angew