const-correctness

How to call a non-const method from a const method?

主宰稳场 提交于 2019-12-18 14:56:03
问题 I've got a const method in my class, which cannot be changed to non-const. In this method, I need to call a non-const method but the compiler doesn't let me do that. Is there any way around it? Here is a simplified sample of my code: int SomeClass::someMethod() const { QColor saveColor = color(); setColor(QColor(255,255,255)); // Calling non-const method // .... setColor(saveColor); // restore color return 1; } 回答1: One of the challenges of doing const -correctness is you can't do it halfway.

Does it ever make sense to make a fundamental (non-pointer) parameter const?

我的未来我决定 提交于 2019-12-18 12:18:51
问题 I recently had an exchange with another C++ developer about the following use of const : void Foo(const int bar); He felt that using const in this way was good practice. I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with regard to overwrite). In addition, doing this prevents the implementer of Foo from modifying their private copy of the argument. So, it both mandates and advertises

Why no 'const' in Python? [closed]

帅比萌擦擦* 提交于 2019-12-17 16:03:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I come from C background and am learning Python. The lack of explicit type-safety is disturbing, but I am getting used to it. The lack

Is there some ninja trick to make a variable constant after its declaration?

泪湿孤枕 提交于 2019-12-17 09:38:15
问题 I know the answer is 99.99% no, but I figured it was worth a try, you never know. void SomeFunction(int a) { // Here some processing happens on a, for example: a *= 50; a %= 10; if(example()) a = 0; // From this point on I want to make "a" const; I don't want to allow // any code past this comment to modify it in any way. } I can do something somewhat similar with const int b = a; , but it's not really the same and it creates a lot of confusion. A C++0x-only solution is acceptable. EDIT :

Why am I getting an error converting a ‘float**’ to ‘const float**’?

こ雲淡風輕ζ 提交于 2019-12-17 06:45:44
问题 I have a function that receives float** as an argument, and I tried to change it to take const float** . The compiler ( g++ ) didn't like it and issued : invalid conversion from ‘float**’ to ‘const float**’ this makes no sense to me, I know (and verified) that I can pass char* to a function that takes const char* , so why not with const float** ? 回答1: See Why am I getting an error converting a Foo** → const Foo**? Because converting Foo** → const Foo** would be invalid and dangerous ... The

Using boost::optional with constant types - C++

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 12:46:21
问题 I have a container class which uses boost::optional to hold the value. Here is the code looks like, template<typename T> struct traits { typedef T value_type; typedef T& reference; }; template<typename T> struct traits<const T> { typedef const T value_type; typedef const T& reference; }; template<typename T> struct traits<T*> { typedef T* value_type; typedef T* reference; }; template<typename T> struct traits<const T*> { typedef const T* value_type; typedef const T* reference; }; template

How do I require const_iterator semantics in a template function signature?

旧城冷巷雨未停 提交于 2019-12-12 09:38:40
问题 I am creating a constructor that will take a pair of input iterators. I want the method signature to have compile-time const semantics similar to: DataObject::DataObject(const char *begin, const char *end) However, I can't find any examples of this. For example, my STL implementation's range constructor for vector is defined as: template<class InputIterator> vector::vector(InputIterator first, InputIterator last) { construct(first, last, iterator_category(first)); } which has no compile-time

Is it a good practice to free memory via a pointer-to-const

纵然是瞬间 提交于 2019-12-12 09:35:39
问题 There are many questions discussing the details of C and C++ dealing with pointer-to-const deletion, namely that free() does not accept them and that delete and delete[] do and that constness doesn't prevent object destruction. What I am interested on is whether you think it is a good practice to do so, not what the languages (C and C++) allow. Arguments for pointer-to-const deletion include: Linus Torvalds' kfree() , unlike C's free() , takes a void const* argument because he thinks that

Trailing return types, decltype and const-ness

▼魔方 西西 提交于 2019-12-12 07:25:47
问题 I was merily experimenting with the new trailing return types, where I hit a problem with this (simplified) code #include <list> class MyContainer{ std::list<int> ints; auto begin( ) -> decltype(ints.begin()) { return ints.begin(); } auto begin( ) const -> decltype(ints.begin()) { return ints.begin(); } }; Ignore the fact of how pointless this code is. The important part is the compiler error generated when using GCC 4.6.1 (with -std=c++0x flag): In member function 'std::list<int>::iterator

If class A modifies its construction parameters, can I initialize const A's with const parameters?

时光毁灭记忆、已成空白 提交于 2019-12-12 03:44:20
问题 Suppose I have class A final { int& ir; public: A(int& x) : ir(x) { } void set(int y) { ir = y; } // non-const method! int get() const { return ir; } }; and const int i; Obviously I can't have A a(i); since that would breaks constness. But I also cannot have const A a(i); despite the fact that this will not break constness de-facto. C++ doesn't support "const-only" ctors, e.g. in this case one which would take a const int& . Is there a way to get const A a wrapping a reference to i - other