documentation of boost::shared_ptr说:



但是我没有发现任何写入的地方,如果这样做会增加引用计数器。

我尝试了以下代码和it works:

struct A {
    virtual int foo() {return 0;}
};

struct B : public A {
    int foo() {return 1;}
};

int main() {
    boost::shared_ptr<A> a;
    {
        boost::shared_ptr<B> b(new B());
        a = b;
    }

    std::cout << a->foo() << std::endl; ///Prints 1
}

因此,假设情况总是如此,但是我找不到可以证实这一点的信息来源。

最佳答案

好的,当我写完问题后,我终于在copy constructor documentation中找到了答案



因此,答案是

10-07 19:29
查看更多