我有几个容器中的一类,对于包含 shared_ptr的对居住在堆中的对象实例,载体或地图。C++智能指针const正确性
例如
template <typename T>
class MyExample
{
public:
private:
vector<tr1::shared_ptr<T> > vec;
map<tr1::shared_ptr<T> , int> h;
};
我想有这个类,有时会返回shared_ptrs 为const对象(通过shared_ptr<const T>
)的公共接口,有时shared_ptr<T>
其中 我允许呼叫者突变的对象。我想要逻辑常量的正确性,所以如果我将 方法标记为const,它不能更改堆上的对象。
问题:
1)我由tr1::shared_ptr<const T>
和tr1::shared_ptr<T>
互换性混淆。 当有人经过一个shared_ptr<const T>
shared_ptr的进级,做我把它存为载体内shared_ptr<T>
或shared_ptr<const T>
和地图或修改我的地图,矢量类型(例如insert_elemeent(shared_ptr<const T>
OBJ)?
2)它是更好地实例化类如下:MyExample<const int>
?这似乎 不适当的限制,因为我永远不会返回shared_ptr<int>
?
“l值”。一个l值不需要赋值! – curiousguy 2011-12-15 06:36:30