I have this code sample:
class Number
{
int i;
public:
Number(int i1): i(i1) {}
operator int() const {return i;}
};
What are the implications of removing the const
modifier from the casting operator?
Does it affect auto casting, and why?
2) const_cast can be used to pass const data to a function that doesn't receive const. For example, in the following program fun() receives a normal pointer, but a pointer to a const can be passed with the help of const_cast. 3) It is undefined behavior to modify a value which is initially declared as const.
The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.
If the conversion operator is not const, you can't convert const objects:
const Number n(5);
int x = n; // error: cannot call non-const conversion operator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With