c++: std::tr1::shared_ptr 来自此

发布于 2024-12-17 09:12:32 字数 1656 浏览 4 评论 0原文

我有以下代码:

#include <memory>

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo
{
public:
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(pFoo_t(this)); }
    void doSomethingElse() { DoSomething::doSomethingElse(pFoo_t(this)); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Foo foo;
    foo.doSomething();
    foo.doSomethingElse();

    return 0;
}

我启动此示例并得到下一个断言:_BLOCK_TYPE_IS_VALID(pHead->nBloakUse)。

我怎样才能避免这种情况?

我使用以下代码来解决这个问题:

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo
{
public:
    void Init(pFoo_t _pFoo) { m_pFoo = _pFoo; }
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(m_pFoo.lock()); }
    void doSomethingElse() { DoSomething::doSomethingElse(m_pFoo.lock()); }
private:
    std::tr1::weak_ptr<Foo> m_pFoo;
};

int _tmain(int argc, _TCHAR* argv[])
{
    {
        Foo * foo = new Foo();
        pFoo_t pFoo(foo);
        foo->Init(pFoo);
        foo->doSomething();
        foo->doSomethingElse();
    }
    return 0;
}

但我认为有更好的解决方案。

I have the following code:

#include <memory>

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo
{
public:
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(pFoo_t(this)); }
    void doSomethingElse() { DoSomething::doSomethingElse(pFoo_t(this)); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Foo foo;
    foo.doSomething();
    foo.doSomethingElse();

    return 0;
}

I start this sample and I get next assert: _BLOCK_TYPE_IS_VALID(pHead->nBloakUse).

How can I avoid this?

I used the following code for resolve this problem:

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo
{
public:
    void Init(pFoo_t _pFoo) { m_pFoo = _pFoo; }
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(m_pFoo.lock()); }
    void doSomethingElse() { DoSomething::doSomethingElse(m_pFoo.lock()); }
private:
    std::tr1::weak_ptr<Foo> m_pFoo;
};

int _tmain(int argc, _TCHAR* argv[])
{
    {
        Foo * foo = new Foo();
        pFoo_t pFoo(foo);
        foo->Init(pFoo);
        foo->doSomething();
        foo->doSomethingElse();
    }
    return 0;
}

But I think there is a better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-12-24 09:12:32

不要手动执行此操作。让您的类继承 std::enable_shared_from_this 并使用 std::shared_from_this() 获取共享指针。

此外,您应该将对象直接分配到共享指针中:

pFoo_t pFoo = std::make_shared<Foo>();  // good
pFoo_t pFoo(new Foo());                 // legacy

(顺便说一句,您要么为此包含 ,要么使用所有这些的 TR1 版本(然后是no make_shared) 并包含 我知道 MSVC 可以让您避免很多草率的情况,但为了可移植性,您应该选择一个或另一个。)

Don't implement this manually. Make your class inherit from std::enable_shared_from_this and use std::shared_from_this() to get a shared pointer.

Moreover, you should allocate your object right into a shared pointer:

pFoo_t pFoo = std::make_shared<Foo>();  // good
pFoo_t pFoo(new Foo());                 // legacy

(By the way, you either include <memory> for this, or you use the TR1 versions of all this (then there's no make_shared) and include <tr1/memory>. I know that MSVC lets you get away with lots of sloppiness, but for the sake of portability you should pick one or the other.)

烂柯人 2024-12-24 09:12:32

感谢您的回复。

你是对的。

现在正确的代码如下:

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo : public std::enable_shared_from_this<Foo> 
{
public:
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(this->shared_from_this()); }
    void doSomethingElse() { DoSomething::doSomethingElse(this->shared_from_this()); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    {
        auto pFoo = std::make_shared<Foo>();
        pFoo->doSomething();
        pFoo->doSomethingElse();
    }
    return 0;
}

Thanks for your reply.

You were right.

The correct code is now as follows:

class Foo;
typedef std::tr1::shared_ptr<Foo> pFoo_t;

class DoSomething
{
public:
    static void doSomething( pFoo_t p) { printf( "doing something...\n"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...\n"); }
};

class Foo : public std::enable_shared_from_this<Foo> 
{
public:
    Foo() { printf( "foo()\n"); }
    ~Foo() { printf( "~foo()\n"); }
public:
    void doSomething() { DoSomething::doSomething(this->shared_from_this()); }
    void doSomethingElse() { DoSomething::doSomethingElse(this->shared_from_this()); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    {
        auto pFoo = std::make_shared<Foo>();
        pFoo->doSomething();
        pFoo->doSomethingElse();
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文