I can\'t find much information on const_cast. The only info I could find (on Stack Overflow) is:
The
const_cast<>()i
What Adam says. Another example where const_cast can be helpful:
struct sample {
T& getT() {
return const_cast(static_cast(this)->getT());
}
const T& getT() const {
/* possibly much code here */
return t;
}
T t;
};
We first add const to the type this points to, then we call the const version of getT, and then we remove const from the return type, which is valid since t must be non-const (otherwise, the non-const version of getT couldn't have been called). This can be very useful if you got a large function body and you want to avoid redundant code.