Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ difference between adding const-ness with static_cast and const_cast of "this" object?

As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast<const A&>(*this).Methodology(); however, in accidental usage due to an overzealous Visual Assist X Intellisense I typed: const_cast<const A&>(*this).Methodology(); and it worked just fine.

What are any and all differences in this case with using a particular cast?

IDE in use: Visual Studio 2010.

like image 405
Casey Avatar asked Jun 20 '12 20:06

Casey


People also ask

What is difference between static_cast and Dynamic_cast?

static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. dynamic_cast −This cast is used for handling polymorphism.

When should I use const_cast?

const_cast is one of the type casting operators. It is used to change the constant value of any object or we can say it is used to remove the constant nature of any object. const_cast can be used in programs that have any object with some constant value which need to be changed occasionally at some point.

Why do we need static_cast in C++?

In C++ the static_cast<>() will allow the compiler to check whether the pointer and the data are of same type or not. If not it will raise incorrect pointer assignment exception during compilation.

Can you const static cast?

The static_cast operator cannot be used to cast away const. You can use static_cast to cast “down” a hierarchy (from a base to a derived pointer or reference), but the conversion is not checked; the result might not be usable. A static_cast cannot be used to cast down from a virtual base class.


1 Answers

Assuming that the type of this is A*, there is no difference.

In general const_cast can cast aways the const specifier (from any level of indirection or template parameter)

static_cast<> can cast a type to another if the target type is in the source's type hierarchy.

They cannot do each other's work.

The reason they both worked in your case is because you have introduced const-ness, as opposed to having taken it away (calling from the non-const version of the function the type of this is A*, no const). You could just as well have written

const A& tmp = *this;
tmp.Methodology();

and it would have worked without the need for any casting. The casting is used for convenience and terseness to not have to introduce a new variable.

Note: you can use static_cast<> here as you know that you are casting to the right type. In other cases (when you cannot be sure) you need to use dynamic_cast<> that does a runtime type check to ensure the conversion is valid

like image 61
Attila Avatar answered Oct 30 '22 17:10

Attila