c++: std::tr1::shared_ptr 来自此
我有以下代码:
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要手动执行此操作。让您的类继承
std::enable_shared_from_this
并使用std::shared_from_this()
获取共享指针。此外,您应该将对象直接分配到共享指针中:
(顺便说一句,您要么为此包含
,要么使用所有这些的 TR1 版本(然后是nomake_shared
) 并包含
我知道 MSVC 可以让您避免很多草率的情况,但为了可移植性,您应该选择一个或另一个。)Don't implement this manually. Make your class inherit from
std::enable_shared_from_this
and usestd::shared_from_this()
to get a shared pointer.Moreover, you should allocate your object right into a shared pointer:
(By the way, you either include
<memory>
for this, or you use the TR1 versions of all this (then there's nomake_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.)感谢您的回复。
你是对的。
现在正确的代码如下:
Thanks for your reply.
You were right.
The correct code is now as follows: