0

Is there anything wrong with using const_cast in situations like this?

class List
{
public:
    const List * head () const
    {
        // seek to the head of the list
    }

    List * head ()
    {
        return const_cast <List *> (((const List *)this) -> head ());
    }
};

It seems clear to me that, assuming there are no strange tricks in the const version of the function, it should always be safe to defer to it in the non-const version (but not vice-versa of course).

Are there any nasty surprises in the language that would ever make this a bad idea?

spraff
  • 29,265
  • 19
  • 105
  • 197

0 Answers0