10

all: this is quoted from Effective C++ 3rd editiion

const_cast is typically used to cast away the constness of objects. It is the only C++-style cast that can do this.

My question is can const_cast add constness to a non-const object? Actually i wrote a small programme trying to approve my thought.

class ConstTest
{
 public:

 void test() {
    printf("calling non-const version test const function \n");
}

 void test() const{
    printf("calling const version test const function \n");

} 

};
 int main(int argc,char* argv){
 ConstTest ct;
 const ConstTest cct;
 cct.test();
 const_cast<const ConstTest&>(ct).test();//it is wrong to write this statement without the '&',why

}

Omitting the '&' incurs error below:

error C2440: 'const_cast' : cannot convert from 'ConstTest' to 'const ConstTest'

It shows that const_cast can add constness,but seems like you have to cast to a object reference, what is the magic of this reference ?

Tracy
  • 1,988
  • 5
  • 25
  • 36
  • Why would you use const_cast to add const? Just assign the non-const object to a const variable and you are done. I've no clue why the const reference works in your case though. – Karel Petranek Sep 27 '10 at 12:01
  • 2
    Suppose a member function has a const and non-const override: how do you call the const override on a non-const object without making a copy? (The answer is `static_cast`, but it's not immediately clear to my why `const_cast` shouldn't also work for this) – Tyler McHenry Sep 27 '10 at 12:04
  • @Tyler: you cast to a const reference, for instance with `static_cast`. – Alexandre C. Sep 27 '10 at 12:05
  • 1
    @Alexandre Yeah, I know that's how you do it, but this question made me curious as to why the C++ standard chooses not to allow `const_cast` for this purpose, since the logical purpose of such a cast is to alter the const-ness of a variable. – Tyler McHenry Sep 27 '10 at 12:07

4 Answers4

8

You don't need const_cast to add constness:

class C;
C c;
C const& const_c = c;

The other way around needs a const_cast though

const C const_c;
C& c = const_cast<C&>(const_c);

but behavior is undefined if you try to use non-const operations on c.

By the way, if you don't use a reference, a copy of the object is to be made:

C d = const_c; // Copies const_c
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • 1
    Still, adding `const` _sometimes_ is a useful thing to do, because it allows more concise code than through adding an additional (reference) variable. See the lower half of this page: http://books.google.de/books?id=mT7E5gDuW_4C&pg=PA188. Note that it can, as [sharptooth correctly noted](http://stackoverflow.com/questions/3803521/question-about-const-cast-in-c/3803545#3803545), only be used to cast to a `const` _reference_. – sbi Sep 27 '10 at 14:32
  • why casting away the constness of const_c is undefined? What if it is a built-in type,i.e. char,int,bool,etc ? – Tracy Sep 27 '10 at 15:48
  • Behavior isn't undefined, unless you try to use the non-const reference to modify the const object. – Steve Jessop Sep 27 '10 at 16:08
3

const_cast can also be used to add constness.

$5.2.11/3 - "For two pointer types T1 and T2 where

T1 is cv1 , 0 pointer to cv1 , 1 pointer to . . . cv1 ,n − 1 pointer to cv1 ,n T
and
T2 is cv2 , 0 pointer to cv2 , 1 pointer to . . . cv2 ,n − 1 pointer to cv2 ,n T

where T is any object type or the void type and where cv1 ,k and cv2 ,k may be different cv-qualifications, an rvalue of type T1 may be explicitly converted to the type T2 using a const_cast. The result of a pointer const_cast refers to the original object.

Consider:

int *t1 = NULL;  // T = int, cv1, 0 = none
int * const t2 = const_cast<int * const>(t1);   // T = int, cv2, 0 = const

As per the quote above, the above is fine and it adds constness to t

But as Herb Sutter says, it probably is not required explicitly to be done most of the times.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • I have read quite a few books, none of them ever mentioned using const_cast to add constness to an object or object reference? However, what i encountered is using static_cast to achieve the same as introduced in Effective C++ by Scott Meyers.OOps,could not link to the google book – Tracy Sep 29 '10 at 17:24
  • @Tracy: http://lookdit.wordpress.com/2010/05/27/c-common-knowledge-by-stephen-c-dewherst/ (Check Item 9) – Chubsdad Sep 30 '10 at 02:17
2

const_cast can only be used to cast to pointers and references. It can't be used to cast to objects. Here's why: if you have a const object you can't make it non-const and vice versa - it's already const, you can't redeclare it. You can only try to access it through a pointer or reference without (or with) const.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

const_cast cannot modify the constness of the value. So it returns a const reference to the value.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93