"David White" <no@email.provi ded> wrote in message
news:sL******** ***********@nas al.pacific.net. au...
"johny smith" <pr************ **@charter.net> wrote in message
news:10******** *****@corp.supe rnews.com... Can someone please give me some good reference site/information for
understanding when and why I would use
1.) const_cast
Don't know why you would do this?
It's for casting from const to non-const.
I guess now that you already knew that. I'm struggling to come up with
necessary uses for it, or uses that don't indicate a flawed design.
Stroustrup's "The C++ Programming Language" gives an example of a class
member used to cache a value. Even in a const member function you might want
to change the value of the cache, since the cache only affects performance
and not the logical state of the object. Changing a class member in a const
member function requires const_cast. However, I don't think this is a good
example because the member used as the cache would be better declared
'mutable', obviating the need for the const_cast. I did manage to find this
rather unsual case in my own code that uses both const_cast and
reinterpret_cas t:
class ParamDB
{
// A database class
};
// A handle used to access the database across a C interface
typedef struct DBHandleStruct_ {} *DBHandle;
DBHandle asDBHandle(cons t ParamDB *pDB)
{
return reinterpret_cas t<DBHandle>(con st_cast<ParamDB *>(pDB));
}
I suppose I could have (or should have) made DBHandle const struct
DBHandleStruct_ {} * and I still wouldn't have needed the const_cast.
DW