const-correctness

Const correctness: const char const * const GetName const (//stuff);

一个人想着一个人 提交于 2019-11-28 04:25:00
问题 Labelled as homework because this was a question on a midterm I wrote that I don't understand the answer to. I was asked to explain the purpose of each const in the following statement: const char const * const GetName() const { return m_name; }; So, what is the explanation for each of these consts? 回答1: Take them from the right. The one before the ; tells the client this is a design level const i.e. it does not alter the state of the object. (Think of this as a read-only method.) Okay, now

Why there is no concept of “const-correctness” for class's static member functions?

[亡魂溺海] 提交于 2019-11-28 03:01:50
问题 Use case: class A { static int s_common; public: static int getCommon () const { s_common; }; }; Typically this results in an error as: error: static member function ‘static int A::getCommon()’ cannot have cv-qualifier This is because const ness applies only to the object pointed by this , which is not present in a static member function. However had it been allowed, the static member function's "const"ness could have been easily related to the static data members. Why is this feature is not

Logical const in D

半城伤御伤魂 提交于 2019-11-28 00:49:00
D has two types of constness: immutable variables are ones that were declared immutable, and always will be immutable, while const variables are simply read only versions of an object. Logical const is when a function is marked as const , but allows write access to one or more member variables. The typical use of this is for lazy evaluation, e.g. (in C++) struct Matrix { double determinant() const { if ( m_dirty ) { m_determinant = /* expensive calculation */; m_dirty = false; } return m_determinant; } void set(int i, int j, double x) { m_dirty = true; ...; } mutable bool m_dirty; mutable

Why no 'const' in Python? [closed]

此生再无相见时 提交于 2019-11-27 21:01:51
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 of built-in contract-based programming (pure abstract classes, interfaces) is something to get used to, in the face of all the advantages of a dynamic language. However, the inability to request const-cortectness is driving me crazy! Why are there no constants in Python? Why are class-level constants discouraged? C and Python belongs to two different classes of languages. The former one is statically typed. The latter is dynamic . In a statically typed language

Idiomatic Way to declare C++ Immutable Classes

假如想象 提交于 2019-11-27 19:19:18
So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const. struct RockSolid { const float x; const float y; float MakeHarderConcrete() const { return x + y; } } Is this actually the way "we should do it" in C++? Or is there a better way? The way you proposed is perfectly fine, except if in your code you need to make assignment of RockSolid variables, like this: RockSolid a(0,1); RockSolid b(0,1); a = b; This would not work as the

Is “const LPVOID” equivalent to “void * const”?

匆匆过客 提交于 2019-11-27 16:33:16
问题 And if so, why some Win32 headers use it? For instance: BOOL APIENTRY VerQueryValueA( const LPVOID pBlock, LPSTR lpSubBlock, LPVOID * lplpBuffer, PUINT puLen ); A bit more elaboration: If the API never uses references (or any other C++-only constructs) but only pointers and values, what is the point of having const LPVOID vs. LPCVOID . Should I treat every place I see const LPVOID as some place where the real meaning is LPCVOID ? (and thus it is safe to add a cast) Further clarification: It

Const correctness for array pointers?

梦想的初衷 提交于 2019-11-27 08:53:44
Someone made an argument saying that in modern C, we should always pass arrays to functions through an array pointer, since array pointers have strong typing. Example: void func (size_t n, int (*arr)[n]); ... int array [3]; func(3, &array); This sounded like it could potentially be a good idea to prevent all kinds of type-related and array-out-of-bounds bugs. But then it occurred to me I don't know how to apply const correctness to this. If I do void func (size_t n, const int (*arr)[n]) then it is const correct. But then I can no longer pass the array, because of incompatible pointer types.

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

你。 提交于 2019-11-27 08:11:55
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 : another less abstracted example, the one that made me ask this question: void OpenFile(string path) {

about const member function [duplicate]

喜欢而已 提交于 2019-11-27 06:57:44
问题 This question already has an answer here: What is the meaning of a const at end of a member function? [duplicate] 3 answers I met two explanation of const member function class A{ public: ... void f() const {} ... } it means it could only access constant members; it means it does not modify any members; I think the second one is right. But why does the first one come out? Is there anything to be clarify? Thanks! 回答1: You can examine all class member values in a const member function, and in

How to call a non-const function within a const function (C++)

陌路散爱 提交于 2019-11-27 05:54:10
问题 I have a legacy function that looks like this: int Random() const { return var_ ? 4 : 0; } and I need to call a function within that legacy code so that it now looks like this: int Random() const { return var_ ? newCall(4) : 0; } The problem is that I'm getting this error: In member function 'virtual int Random() const': class.cc:145: error: passing 'const int' as 'this' argument of 'int newCall(int)' discards qualifiers Now I know in order to fix this error I can make my newCall() a const