English 中文(简体)
c++: st:
原标题:c++: std::tr1::shared_ptr from this

我有以下法典:

#include <memory>

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

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

class Foo
{
public:
    Foo() { printf( "foo()
"); }
    ~Foo() { printf( "~foo()
"); }
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...
"); }
    static void doSomethingElse( pFoo_t p) { printf( "doing something else...
"); }
};

class Foo
{
public:
    void Init(pFoo_t _pFoo) { m_pFoo = _pFoo; }
    Foo() { printf( "foo()
"); }
    ~Foo() { printf( "~foo()
"); }
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;
}

但我认为有一个更好的解决办法。

最佳回答

• 自行执行。 您的班子继承std:enable_commd_from_this,并使用std:commd_from_this(),以获得共同点。

此外,你应将物体权分配给一个共同点:

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.)

问题回答

感谢您的答复。

你是正确的。

正确的法典如下:

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

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

class Foo : public std::enable_shared_from_this<Foo> 
{
public:
    Foo() { printf( "foo()
"); }
    ~Foo() { printf( "~foo()
"); }
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;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签