2009-10-20 09:54:32 +02:00
|
|
|
The tt(const) keyword has been given a special place in casting. Normally
|
|
|
|
anything tt(const) is tt(const) for a good reason. Nonetheless situations
|
|
|
|
may be encountered where the tt(const) can be ignored. For these special
|
|
|
|
situations the tt(const_cast) should be used. Its syntax is:
|
2009-10-19 17:00:38 +02:00
|
|
|
centt(const_cast<type>(expression))
|
2009-10-20 09:54:32 +02:00
|
|
|
A ti(const_cast<type>(expression)) expression is used to undo the
|
2009-10-25 17:44:33 +01:00
|
|
|
tt(const) attribute of a (pointer) type.
|
2009-10-19 17:00:38 +02:00
|
|
|
|
2009-10-20 09:54:32 +02:00
|
|
|
The need for a tt(const_cast) may occur in combination with functions from
|
|
|
|
the standard bf(C) library which traditionally weren't always as const-aware
|
|
|
|
as they should. A function tt(strfun(char *s)) might be available, performing
|
|
|
|
some operation on its tt(char *s) parameter without actually modifying the
|
|
|
|
characters pointed to by tt(s). Passing tt(char const hello[] = "hello";) to
|
|
|
|
tt(strfun) will produce the warning
|
|
|
|
centt(passing `const char *' as argument 1 of `fun(char *)' discards const)
|
|
|
|
A tt(const_cast) is the appropriate way to prevent the warning:
|
|
|
|
centt(strfun(const_cast<char *>(hello));)
|