将static_pointer_cast <derived>添加到std :: list <shared_ptr <base >>会导致称为错误的纯虚拟方法

时间:2019-04-24 13:53:42

标签: c++ casting virtual shared-ptr

我有这个基础课

class Base {

    std::string nome;

public:
    std::string getName() const;
    virtual int mType() const = 0;
    virtual void ls(int indent=0) const = 0;
};

我从中派生此Directory类的

class Directory : public Base {

private:
    static std::shared_ptr<Directory> root;
    std::list<std::shared_ptr<Base>> childs;
    std::weak_ptr<Directory> parent;
    std::weak_ptr<Directory> thisDirectory;
    std::string nome;

protected:
    Directory(const std::string n);

public:
    static std::shared_ptr<Directory> getRoot();
    std::shared_ptr<Directory> addDirectory(std::string nome);
    std::shared_ptr<File> addFile(std::string nome, uintmax_t size);
    std::shared_ptr<Base> get(std::string name);
    std::shared_ptr<Directory> getDir(std::string name);
    std::shared_ptr<File> getFile(std::string name);
    void remove(std::string nome);

    int mType() const override;
    void ls(int indent=0) const override;
};

addDirectory方法

std::shared_ptr<Directory> Directory::addDirectory(std::string nome) {
    auto it = std::find_if(childs.begin(), childs.end(), [nome](std::shared_ptr<Base> p){return (p->getName()==nome);});
    if(it == childs.end()){
        std::cout<<"creating "<<nome<<" directory"<<std::endl;
        std::shared_ptr<Directory> p = std::shared_ptr<Directory>(new Directory(nome));
        childs.push_back(std::static_pointer_cast<Base>(p));
        p->parent = std::shared_ptr<Directory>(this);
        std::cout<<nome<<" created"<<std::endl;
        return p;
    }
    else {
        // todo gestione eccezione
        std::cout<<nome<<" already exists"<<std::endl;
        throw std::exception();
    }
}

将打印此输出

pure virtual method called
creating root...
terminate called recursively
root created
creating alfa directory
alfa created

pure virtual method calledchilds.push_back(std::static_pointer_cast<Base>(p));引起,而terminate called recursivelyp->parent = std::shared_ptr<Directory>(this);引发。 使用childs.push_back(p)可以正常工作,为什么? 它试图建立基础对象的实例吗?我可以通过对基类使用shared_ptr来管理派生类对象的列表吗?

1 个答案:

答案 0 :(得分:0)

您必须使用enable_shared_from_this才能从shared_ptr创建一个this

将您的类声明更改为...

class Directory : public Base, public std::enable_shared_from_this<Directory>

,并将std::shared_ptr<Directory>(this)替换为shared_from_this()。还要确保所有目录(包括根目录)都是使用共享指针构造的。