C ++如何在tr1 :: function中使用shared_ptr?

时间:2019-08-02 12:07:23

标签: c++ visual-studio-2010

我正在绑定成员函数对象,并且正在接收std :: tr1 :: function。 现在,我在另一个C ++ / CLI项目中使用了指向该std :: tr1 :: function的指针,在这里我遇到了对象破坏和行为不确定的问题(因为使用了指向std :: tr1 :: function的指针)。为了避免这种情况,我尝试改用shared_ptr。我该怎么办?

ITestInterface.h

#pragma once

class ITestInterface
{
public:
  virtual void Test() = 0;
};

TestNativeClass.h

#pragma once
#include <functional>
#include "ITestInterface.h"
#include <memory>

class TestNativeClass
{
public:
  TestNativeClass(void);

private:
  void testHandle(std::shared_ptr<ITestInterface> test);

private:
  std::tr1::function<void __cdecl (std::shared_ptr<ITestInterface> test)> m_datahandle;
};

TestNativeClass.cpp

#include "TestNativeClass.h"
#include <memory>


TestNativeClass::TestNativeClass(void)
{
  m_datahandle = std::tr1::bind(&TestNativeClass::testHandle, this, std::tr1::placeholders::_1);
  // This below doesn't work and says:error C2440: '<function-style-cast>' : cannot convert from 'std::tr1::function<_Fty>' to 'std::tr1::shared_ptr<_Ty>'
  std::shared_ptr<std::tr1::function<void __cdecl (std::shared_ptr<ITestInterface> test)>> test = std::shared_ptr<std::tr1::function<void __cdecl (std::shared_ptr<ITestInterface> test)>>(m_datahandle);
}

void TestNativeClass::testHandle(std::shared_ptr<ITestInterface> test)
{

}

0 个答案:

没有答案