简体   繁体   English

std :: shared_ptr和boost :: shared_ptr的区别

[英]std::shared_ptr & boost::shared_ptr difference

I've the following code: 我有以下代码:

// interface.h
#ifndef INTERFACE_H
#define INTERFACE_H    

#include <memory>    
class IInterface {
public:
    virtual ~IInterface() = 0;
    virtual void doSomething() = 0;
};
inline IInterface::~IInterface() {}

typedef std::shared_ptr< IInterface > IIntPtr;    
IIntPtr makeInterface();    
#endif // INTERFACE_H


// impl.cpp
#include "interface.h"

class Interface : public IInterface { public:
    Interface() {}
    ~Interface() {}
    void doSomething() {} };

IIntPtr makeInterface() { return IIntPtr( new Interface() ); }

// main.cpp
#include "interface.h"
using namespace std;

int main()
{
    auto p = makeInterface();
    p->doSomething();
}

Now: if i use typedef std::shared_ptr< IInterface > IIntPtr; 现在:如果我使用typedef std::shared_ptr< IInterface > IIntPtr; all is fine, but if I use typedef boost::shared_ptr< IInterface > IIntPtr; 一切都很好,但是如果我使用typedef boost::shared_ptr< IInterface > IIntPtr; I've some compiler error: 我有一些编译器错误:

  • Compiler Error C2027: A type cannot be used until it is defined. 编译器错误C2027:在定义类型之前,不能使用该类型。 To resolve the error, be sure the type is fully defined before referencing it. 要解决该错误,请在引用类型之前确保类型已完全定义。
  • Compiler Error C2079: 'identifier' uses undefined class/struct/union 'name' 编译器错误C2079:“标识符”使用未定义的类/结构/联合“名称”
  • Compiler Error C2440: The compiler cannot cast from 'type1' to 'type2'. 编译器错误C2440:编译器无法从'type1'强制转换为'type2'。

I'm using msvc10, but the code must compile with msvc9.0 so I have to use Boos.SmartPtr 我正在使用msvc10,但是代码必须与msvc9.0一起编译,因此我必须使用Boos.SmartPtr

Am I missing something? 我想念什么吗?

OP在评论中回答了他自己的问题:在尝试Boost版本时,他忘记了#include <boost/shared_ptr.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM