const-correctness

Why is const-correctness specific to C++?

放肆的年华 提交于 2019-11-29 20:53:23
Disclaimer: I am aware that there are two questions about the usefulness of const-correctness, however, none discussed how const-correctness is necessary in C++ as opposed to other programming languages. Also, I am not satisfied with the answers provided to these questions. I've used a few programming languages now, and one thing that bugs me in C++ is the notion of const-correctness. There is no such notion in Java, C#, Python, Ruby, Visual Basic, etc., this seems to be very specific to C++. Before you refer me to the C++ FAQ Lite, I've read it, and it doesn't convince me. Perfectly valid,

What is meaning of a pointer to a constant function?

荒凉一梦 提交于 2019-11-29 10:54:40
问题 Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers. Here are some questions: What is the meaning of a pointer to a constant function versus a pointer to a non-constant function? Can a function be const? Can a function be non-const (mutable)? What is the proper

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

空扰寡人 提交于 2019-11-29 10:42:05
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? 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 the return value: const char const *const This is a constant pointer to okay ... here we go boom! You have an

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

拟墨画扇 提交于 2019-11-29 09:32:47
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 present in C++; any logical reason behind it ? cv-qualifiers affect the function's signature. So you

Invoking a nonconst method on a member from a const method

情到浓时终转凉″ 提交于 2019-11-29 07:12:57
I was surprised to find this "hole" in "const"ness: #include <stdio.h> class A { int r ; public: A():r(0){} void nonconst() { puts( "I am in ur nonconst method" ) ; r++; } } ; class B { A a ; A* aPtr ; public: B(){ aPtr = new A() ; } void go() const { //a.nonconst() ; // illegal aPtr->nonconst() ; //legal } } ; int main() { B b ; b.go() ; } So basically from const method B::go() , you can invoke the non-const member function (aptly named nonconst() ) if object of type A is referenced by a pointer. Why is that? Seems like a problem (it kind of was in my code, where I found it.) When and object

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

早过忘川 提交于 2019-11-29 02:01:30
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 appears that const LPVOID pBlock was indeed a mistake in this case. Windows 2008 SDK replaces it to

What is the best smart pointer return type for a factory function?

跟風遠走 提交于 2019-11-28 15:38:11
问题 With respect to smart pointers and new C++11/14 features, I am wondering what the best-practice return values and function parameter types would be for classes that have these facilities: A factory function (outside of the class) that creates objects and returns them to users of the class. (For example opening a document and returning an object that can be used to access the content.) Utility functions that accept objects from the factory functions, use them, but do not take ownership. (For

about const member function [duplicate]

 ̄綄美尐妖づ 提交于 2019-11-28 12:11:43
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! You can examine all class member values in a const member function, and in some cases you can even change the value of member variables. The first explanation is incorrect, I don't

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

Deadly 提交于 2019-11-28 10:46:28
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 function. But then I have several funciton calls in newCall() that I have to make, so now I would have to

Can a heap-allocated object be const in C++?

China☆狼群 提交于 2019-11-28 07:32:37
In C++ a stack-allocated object can be declared const : const Class object; after that trying to call a non-const method on such object is undefined behaviour: const_cast<Class*>( &object )->NonConstMethod(); //UB Can a heap-allocated object be const with the same consequences? I mean is it possible that the following: const Class* object = new Class(); const_cast<Class*>( object )->NonConstMethod(); // can this be UB? is also undefined behaviour? Yes. It's legal to construct and destroy a const heap object. As with other const objects, the results of manipulating it as a non- const object (e