具有可变内容的不可变容器 [英] Immutable container with mutable content

查看:119
本文介绍了具有可变内容的不可变容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个故事从我认为很简单的事情开始:



我需要设计一个使用一些STL容器的类。我需要给类的用户访问那些容器的不可变版本。我不希望用户能够更改容器(例如,他们不能在列表上 push_back() ),但我希望用户能够更改所包含的对象(获取具有 back() 的元素并修改它):

  class Foo 
{
public:

// [...]

ImmutableListWithMutableElementsType getImmutableListWithMutableElements();

// [...]
};

// [...]

myList = foo.getImmutableListWithMutableElements();
myElement = myList.back();
myElement.change(42); // OK

// [...]

// myList.push_back(myOtherElement); //不可能

乍一看,似乎有一个 const容器会做。但是,当然,您只能在 const container 上使用 const iterator ,并且您无法更改内容。



<乍一看,诸如专用容器或迭代器之类的东西就出现了。我可能会结束了。



然后,我的想法是有人必须这样做!或一个优雅的,通用的解决方案必须存在!我在这里问我对SO的第一个问题:



如何将标准容器设计/转换为具有可变内容的不可变容器?



我在努力,但我觉得有人会说嘿,我每次都这样做,很容易,看看!,所以我问...



感谢您提供任何提示,建议或精彩的通用方式:)




编辑:



经过一些实验,我最终得到了标准的容器,处理一些特别装饰的智能指针。它接近尼古拉答案。



不可变容器的可变元素的想法不是一个杀人的概念,看到有趣的注释在Oli的答案。



一个特定的迭代器的想法当然是,但它似乎不切实际,因为我需要适应任何类型的容器。



感谢大家的帮助。

解决方案

最简单的选择可能是标准的STL容器 of pointers ,因为 const -ness不会传播到实际的对象。一个问题是,STL不会清理你分配的任何堆内存。有关详情,请参阅 Boost Pointer容器库智能指针


The story begins with something I thought pretty simple :

I need to design a class that will use some STL containers. I need to give users of the class access to an immutable version of those containers. I do not want users to be able to change the container (they can not push_back() on a list for instance), but I want users to be able to change the contained objects (get an element with back() and modify it) :

class Foo
{
    public:

    // [...]

    ImmutableListWithMutableElementsType getImmutableListWithMutableElements();

    // [...]
};

// [...]

myList = foo.getImmutableListWithMutableElements();
myElement = myList.back();
myElement.change(42); // OK

// [...]

// myList.push_back(myOtherElement); // Not possible

At first glance, it seems that a const container will do. But of course, you can only use a const iterator on a const container and you can not change the content.

At second glance, things like specialized container or iterator come to mind. I will probably end up with that.

Then, my thought is "Someone must have done that already !" or "An elegant, generic solution must exist !" and I'm here asking my first question on SO :

How do you design / transform a standard container into an immutable container with mutable content ?

I'm working on it but I feel like someone will just say "Hey, I do that every time, it's easy, look !", so I ask...

Thank you for any hints, suggestions or wonderful generic ways to do that :)


EDIT:

After some experiments, I ended up with standard containers that handle some specifically decorated smart pointers. It is close to Nikolai answer.

The idea of an immutable container of mutable elements is not a killing concept, see the interesting notes in Oli answer.

The idea of a specific iterator is right of course, but it seems not practical as I need to adapt to any sort of container.

Thanks to you all for your help.

解决方案

The simplest option would probably be a standard STL container of pointers, since const-ness is not propagated to the actual objects. One problem with this is that STL does not clean up any heap memory that you allocated. For that take a look at Boost Pointer Container Library or smart pointers.

这篇关于具有可变内容的不可变容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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