c++ - Immutable container with mutable content -


the story begins thought pretty simple :

i need design class use stl containers. need give users of class access immutable version of containers. not want users able change container (they can not push_back() on list instance), want users able change contained objects (get element back() , 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, seems const container do. of course, can use const iterator on const container , can not change content.

at second glance, things specialized container or iterator come mind. end that.

then, thought "someone must have done !" or "an elegant, generic solution must exist !" , i'm here asking first question on :

how design / transform standard container immutable container mutable content ?

i'm working on feel "hey, every time, it's easy, !", ask...

thank hints, suggestions or wonderful generic ways :)


edit:

after experiments, ended standard containers handle decorated smart pointers. close nikolai answer.

the idea of immutable container of mutable elements not killing concept, see interesting notes in oli answer.

the idea of specific iterator right of course, seems not practical need adapt sort of container.

thanks help.

the simplest option standard stl container of pointers, since const-ness not propagated actual objects. 1 problem stl not clean heap memory allocated. take @ boost pointer container library or smart pointers.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -