关键字“this”的智能指针是什么?

问题描述 投票:0回答:0
struct Base
{
    virtual Base* getObject()
    {
        return this;
    }
};

struct Derived : Base
{
    Derived* getObject() override
    {
        return this;
    }
};

什么是智能指针等价于关键字

this
和整个协变返回类型结构?


编辑

当我尝试返回

this
并使用
unique_ptr<Base>
作为返回值时,出现编译时错误,我无法将
Base*
转换为
unique_ptr<Base>

c++ inheritance smart-pointers covariant
© www.soinside.com 2019 - 2024. All rights reserved.