【发布时间】:2011-03-26 18:27:28
【问题描述】:
我的课程包含boost::weak_ptr<T> 的std::set。我有两个函数 begin() 和 end() 将迭代器返回到容器。但是,我不希望客户能够修改T。简单地返回一个const_iterator 是行不通的,因为boost::weak_ptr 指向的T 将是可编辑的。
我想要做的是将const_iterator 返回到std::set<boost::weak_ptr<T const> >。从std::set<boost::weak_ptr<T> >::const_iterator 投射不起作用。有什么办法可以得到我想要的行为吗?
【问题讨论】:
-
您的这两个陈述似乎是矛盾的:1)我不希望客户能够修改 T 和 2)因为 boost 指向的 T ::weak_ptr 将是可编辑的。。这是什么意思?
-
@Nawaz 我的意思是:将
const_iterator返回到std::set<boost::weak_ptr<T> >使得客户端无法修改weak_ptr。但是,他仍然可以从中获取shared_ptr<T>,然后随意修改T。这正是我不希望发生的事情。
标签: c++ boost iterator weak-ptr