To bother or not to bother... "const" as function argument

Started by
31 comments, last by mgarriss 18 years, 10 months ago
Ah, now it's a different issue. It's a question of const member function versus non-const member function. The const qualifier will control which set of member functions you can use. Yes, in that case the const will make a difference, since one of the member functions return an int* while the other returns a const int*. Your original code (both in your latest and in the original post I criticized) let you manipulate an int* regardless.

But whether the class itself contains a pointer or not, and whether it was passed by value or by reference still is irrelevant to the issue (the pointer could be a global variable and it would still be the same). Passing by value, whether const or non-const doesn't allow you to directly modify the original object you passed to the function, since you're operating on a copy of the variable anyway.

One way or the other, if the variable contains a pointer (to non-const), you will be able to use that pointer to modify the pointee, whether the variable was passed by value or by reference, whether it is const or not. A copy of a pointer still points to the same address (shallow copy), not to a copy of the object at the original address (deep copy).

If you have struct Foo { int* ptr; }, then a const Foo object will contain a int* const, not a const int*. It's the pointer itself that is const, not the data it points to, so you can always manipulate the pointee regardless of the const-qualification of the object that contained the pointer. It's only if you want to re-target the pointer that you need to worry. In this case, if the object is const, you will obviously not be able to modify it.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
The reason a compiler may produce more efficient code when dealing with const objects is because it can assume there's no aliasing between them and non-const variables. It cannot make the same assumption for variables later marked as const however, so for code output, it's unlikely it'll make any difference at all whether you put 'const' into your function parameters or not.
this whole thread has had me thinking more about const and the issues surrounding it. i found this interesting tidbit in the standard that i did not know before:
Quote:ISO/IEC FDIS 14882:1998(E)
7.1.5.1 - The cv-qualifiers [dcl.type.cv]
... snip ...
-4- Except that any class member declared mutable (dcl.stc) can be modified, any attempt to modify a const object during its lifetime (basic.life) results in undefined behavior.
... snip ...
-6- For another example
class X {    public:	mutable int i;	int j;};class Y {    public:	X x;	Y();};const Y y;y.x.i++;                        //  well-formed:  mutable  member can be modifiedy.x.j++;                        //  ill-formed:  const-qualified member modifiedY* p = const_cast<Y*>(&y);      //  cast away const-ness of  yp->x.i = 99;                    //  well-formed:  mutable  member can be modifiedp->x.j = 99;                    //  undefined: modifies a  const  member


so all the "i can really modify the const object anyway so there!" people (i've been one before) should know that the standard does not require that it work. in standard-speak it's undefined. so we should watch ourselves. a future compiler release could break old code that relied on this trick and still claim to be standard compiliant.

_ugly

This topic is closed to new replies.

Advertisement