mutable

How can I improve this design that forces me to declare a member function const and declare variables mutable?

匆匆过客 提交于 2019-12-18 08:17:02
问题 For some reason I am iterating over elements of a class in an std::set and would like to slightly modify the keys, knowing that the order will be unchanged. Iterators on std::set are const_iterators because if the key is modified, it might result in a bad order and therefore in set corruption. However I know for sure that my operations won't change the order of my elements in the set. For the moment, here is my solution: class Foo { public: Foo(int a, int b): a_(a),b_(b) {} ~Foo(){} bool

Mutable strings in Python

狂风中的少年 提交于 2019-12-17 15:55:07
问题 Please, do you know of a Python library which provides mutable strings? Google returned surprisingly few results. The only usable library I found is http://code.google.com/p/gapbuffer/ which is in C but I would prefer it to be written in pure Python. Edit: Thanks for the responses but I'm after an efficient library. That is, ''.join(list) might work but I was hoping for something more optimized. Also, it has to support the usual stuff regular strings do, like regex and unicode. 回答1: In Python

Existence of mutable named tuple in Python?

为君一笑 提交于 2019-12-17 15:08:35
问题 Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Camelot import namedgroup Point = namedgroup('Point', ['x', 'y']) p = Point(0, 0) p.x = 10 >>> p Point(x=10, y=0) >>> p.x *= 10 Point(x=100, y=0) It must be possible to pickle the resulting object. And per the characteristics of named tuple, the ordering of the output when represented must match the order

volatile vs. mutable in C++

佐手、 提交于 2019-12-17 14:59:57
问题 I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed? How to use them in different way? Thanks a lot. 回答1: A mutable field can be changed even in an object accessed through a const pointer or reference, or in a const object, so the compiler knows not to stash it in R/O memory. A volatile location is

C++知识点积累(2)

点点圈 提交于 2019-12-17 13:00:26
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1 如果出于某些原因,需要在const成员函数中修改某一个或几个成员,那么可以将需要修改的成员声明为mutable,例如 class A { public: int m_cannotBeModified; mutable int m_needToBeModified; void ModifyMutable() const { m_needToBeModified = 1; //合法 m_cannotBeModified = 1; //不合法 } }; 2 如果一个类没有显式声明构造函数,拷贝构造函数,赋值运算符,析构函数,编译器会相应地合成一个,如果类显式定 义了任何一个构造函数,包括拷贝构造函数,编译器就不会再合成构造函数。 3 要阻止一个类对象拷贝或赋值,可以声明一个简单的基类,基类中将拷贝构造函数和赋值运算符声明为private,这样其派生类的拷贝或赋值操作在编译期就可以被检查出来。例如: class A { protected: A(){} ~A(){} private: A(const A& a){} A& operator=(const A&a){} }; class B : public A { }; int main() { B b; //B b1(b); //编译不通过 //B b2 = b;

Why the “mutable default argument fix” syntax is so ugly, asks python newbie

…衆ロ難τιáo~ 提交于 2019-12-17 07:30:09
问题 Now following my series of "python newbie questions" and based on another question. Prerogative Go to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables and scroll down to "Default Parameter Values". There you can find the following: def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list There's even an "Important

Multiple references in separate lists; Python

ぐ巨炮叔叔 提交于 2019-12-13 05:07:58
问题 I'm trying to basically create references to plot multiple relationships and store them in a list or possibly a dictionary. Basically: variable1 = 10 //in this case, 'ref' denotes that the variable should be a reference) listA = [variable1(ref), variable2, variable3] listB = [variable1(ref), variable4, variable5] for i in listA: i = i + 10 for i in listB: i = i + 10 print listA[0] //Should print 30 print listB[0] //Should print 30 How can I split two references to the same variable into two

Mutable variable available to all classes?

偶尔善良 提交于 2019-12-13 04:37:37
问题 I'm developing a program in C# and need a mutable variable that is available to all classes in my program. For example, I want to set it to a default value, say false , when the program starts, then be able to change it to true later when an action occurs. The true value then needs to be conveyed when other classes read it. How can this be achieved? 回答1: How about a static?: public static class MyProps { public static bool MyProp { get; set; } } In your code: MyProps.MyProp = true; No

Is it possible to access a reference of a struct from a List<T> to make changes?

混江龙づ霸主 提交于 2019-12-13 02:36:09
问题 I have a struct which I put in a List<T> , I want to edit some value in that struct at a specific position. Is this at all possible without making a copy of the struct, editing the copy, and replacing the entry in the List? 回答1: No, to be able to do it you need reference to element of inner array which is not provided by List / IList . You can do that with unsafe code and arrays if you have to. 回答2: From J.Richter's "CLR via C#", 3rd edition: Value types should be immutable: that is, they

Why is that for sqlite3_open we use double pointer ** and for sqlite3_prepare we use pointer *

匆匆过客 提交于 2019-12-13 02:19:42
问题 Please correct me where I understood wrong. I read this answer https://stackoverflow.com/a/833124/5175709 and what I understood was that since the object could expand and run out of space then the memory location my also change.But here two sqlite3 syntaxes are referencing to the object differently. WHY? sqlite3_open we have: sqlite3 **ppDb SQLITE_API int SQLITE_STDCALL sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ And for