数据加载中……

boost::scoped_ptr作为成员变量的使用

原因:scoped_ptr的赋值操作符和拷贝构造函数被声明为了provate,所以它不能被赋值。
boost中scoped_ptr的部分代码:

private:

    T 
* px;

    scoped_ptr(scoped_ptr 
const &);
    scoped_ptr 
& operator=(scoped_ptr const &);

    typedef scoped_ptr
<T> this_type;

    
void operator==( scoped_ptr const& ) const;
    
void operator!=( scoped_ptr const& ) const;

public:

    typedef T element_type;

    
explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
    {
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
        boost::sp_scalar_constructor_hook( px );
#endif
    }

class MyClass{
public:
   MyClass();
private:
  scoped_ptr
<int> m_pScopedPtr;
}

MyClass::MyClass()
:m_pScopedPtr(
new int(100))//这样就可以了
{
}

posted on 2011-07-25 13:46 Stone xin 阅读(771) 评论(2)  编辑 收藏 引用 所属分类: STL&&BOOST

评论

# re: boost::scoped_ptr作为成员变量的使用  回复  更多评论   

不光是重载了赋值操作符和拷贝构造函数的原因才不能被赋值的,是这两个函数被声明为private,因此不能(被调用)被赋值。
2011-07-25 16:10 | megadeath

# re: boost::scoped_ptr作为成员变量的使用  回复  更多评论   

@megadeath
恩,主要原因是声明了private
2011-07-25 16:16 | xinst

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理