1

I was reading about const_cast operator in c++

1.First weird thing thing i can't understand is

const_cast operator syntax i.e.

-const_cast--<--Type-->--(--expression--)--------------------><

what i have understand about this syntax is that it helps to cast away constness of anexpressionof type Type .But consider this code

class  ConstTest {   

private:
    int year;
public:
    ConstTest() : year(2007) {}
    void  printYear() const;
};

int main() {
    ConstTest c;
    c.printYear();
    return  0;
}

void ConstTest::printYear() const {
    ConstTest  *c  = const_cast<ConstTest*>(this);
    c->year  = 42;
    std::cout  <<  "This  is the  year "  << year  << std::endl;
}

Here in line ConstTest *c = const_cast<ConstTest*>(this), I think that the const of this pointer should be cast away, but the output shows that it is the object which this refers to that loses its const-ness.

I feel that the code should have been ConstTest *c = const_cast<ConstTest>(*this), but this produces an error. I know i am wrong at many interpretations. Please correct them all.

2.my second problem is the statement given below

The result of a const_cast expression is an rvalue unless Type is a reference type. In this case, the result is an lvalue.

Why is this so, and why it is not true in case of pointers?

Community
  • 1
  • 1
T.J.
  • 1,466
  • 3
  • 19
  • 35

1 Answers1

4

it helps to cast away constness of an expression of type Type

No, Type is the type of the result, not the type of the operand.

What i think is const of this pointer should be casted away

this has type const ConstTest*. const_cast<ConstTest*>(this) has type ConstTest*. That's what "casting away const" from a pointer-to-const means.

I feel code should have been ConstTest *c = const_cast<ConstTest>(*this)

The result of const_cast<T> has type T, that's how it's defined. Maybe you would have defined it differently, but tough luck, you don't get a ConstTest* by writing const_cast<ConstTest>, you get it by writing const_cast<ConstTest*>. Your preferred syntax is not available.

You can either do ConstTest &c = const_cast<ConstTest&>(*this) or ConstTest *c = const_cast<ConstTest*>(this), so pick your favorite.

The result of a const_cast expression is an rvalue unless Type is a reference type. In this case, the result is an lvalue.

why so and why it is not true in case of pointers?

It is true of pointers. ConstTest* is not a reference type, and the result of const_cast<ConstTest*>(this) is an rvalue. You then assign that value to the variable c.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • Shouldn't `this` be `ConstTest* const`? `const ConstTest*` would mean that the data being pointed to is constant and thus can't be modified. Also what's an example of a ref type? Is the result of `const_cast (*this)` a ref type? This can't be an lvalue since you can't assign to it. – greatwolf Jul 14 '13 at 05:43
  • @greatwolf: I meant what I wrote, in the `printYear` member function, `this` has type `const ConstTest*`. This does mean "pointer-to-const", although having a pointer-to-const does *not* necessarily mean that the data pointed to cannot be modified. It just means you can't modify it through that pointer. The result of `const_cast(*this)` is indeed an lvalue, and has reference type. You are mistaken when you say that if something cannot be assigned to, it therefore must not be an lvalue. There are both modifiable and non-modifiable lvalues. – Steve Jessop Jul 15 '13 at 22:31