C ++智能指针const正确性 [英] C++ smart pointer const correctness

查看:326
本文介绍了C ++智能指针const正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类中的几个容器,例如vector或map,它们包含了
shared_ptr给存在于堆上的对象。



/ p>

  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正确性,所以如果我将
a方法标记为const,它不能更改堆上的对象。



问题:



1)我对 tr1 :: shared_ptr< const T> tr1 :: shared_ptr< ; T>
当有人将 shared_ptr< const T> shared_ptr传递给类时,我将它存储为 shared_ptr< T> shared_ptr shared_ptr< const T& T> obj)?



2)最好是像下面这样实例化类: MyExample< const int& / code>?这似乎
过分限制,因为我不能返回 shared_ptr< int>

解决方案

shared_ptr< T> shared_ptr< const T> / strong>可互换。它以一种方式 - shared_ptr< T> 可以转换为 shared_ptr< const T> p>

观察:

  // f.cpp 

#include< memory>

int main()
{
using namespace std;

shared_ptr< int> pint(new int(4)); // normal shared_ptr
shared_ptr< const int> pcint = pint; // shared_ptr< const T> from shared_ptr< T>
shared_ptr< int> pint2 = pcint; //错误!注释编译
}

通过

编译


cl / EHsc f.cpp


常数。



对于您的第二个问题, MyExample< int> 可能比 MyExample< const int> 更有意义。


I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap.

For example

template <typename T>
class MyExample
{
public:

private:
 vector<tr1::shared_ptr<T> > vec;
 map<tr1::shared_ptr<T> , int> h;
};

I want to have a public interface of this class that sometimes returns shared_ptrs to const objects (via shared_ptr<const T>) and sometimes shared_ptr<T> where I allow the caller to mutate the objects. I want logical const correctness, so if I mark a method as const, it cannot change the objects on the heap.

Questions:

1) I am confused by the interchangeability of tr1::shared_ptr<const T> and tr1::shared_ptr<T>. When someone passes a shared_ptr<const T> shared_ptr into the class, do I store it as a shared_ptr<T> or shared_ptr<const T> inside the vector and map or do I change the map, vector types (e.g. insert_elemeent(shared_ptr<const T> obj) ?

2) Is it better to instantiate classes as follows: MyExample<const int> ? That seems unduly restrictive, because I can never return a shared_ptr<int> ?

解决方案

shared_ptr<T> and shared_ptr<const T> are not interchangable. It goes one way - shared_ptr<T> is convertable to shared_ptr<const T> but not the reverse.

Observe:

// f.cpp

#include <memory>

int main()
{
    using namespace std;

    shared_ptr<int> pint(new int(4)); // normal shared_ptr
    shared_ptr<const int> pcint = pint; // shared_ptr<const T> from shared_ptr<T>
    shared_ptr<int> pint2 = pcint; // error! comment out to compile
}

compile via

cl /EHsc f.cpp

You can also overload a function based on a constness. You can combine to do these two facts to do what you want.

As for your second question, MyExample<int> probably makes more sense than MyExample<const int>.

这篇关于C ++智能指针const正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆