相关疑难解决方法(0)

为什么不能将指向派生类的指针传递给期望引用指向基类的指针的函数?

很抱歉很长的标题,但我确实想要具体.我希望以下代码可以工作,但它没有,我无法弄清楚原因:/

#include <cstdio>
#include <cassert>

class UniquePointer
{
public:
    void Dispose()
    {
        delete this;
    }

    friend void SafeDispose(UniquePointer*& p)
    {
        if (p != NULL)
        {
            p->Dispose();
            p = NULL;
        }
    }
protected:
    UniquePointer() { }
    UniquePointer(const UniquePointer&) { }
    virtual ~UniquePointer() { }
};

class Building : public UniquePointer
{
public:
    Building()
    : mType(0)
    {}
    void SetBuildingType(int type) { mType = type; }
    int GetBuildingType() const { return mType; }
protected:
    virtual ~Building() { }
    int mType;
};

void Foo() …
Run Code Online (Sandbox Code Playgroud)

c++

12
推荐指数
2
解决办法
4449
查看次数

标签 统计

c++ ×1