在这种情况下使用const_cast
有什么问题吗?
class List
{
public:
const List * head () const
{
// seek to the head of the list
}
List * head ()
{
return const_cast <List *> (((const List *)this) -> head ());
}
};
对我来说,似乎很清楚,假设函数的const版本中没有奇怪的技巧,在非const版本中应该始终是安全的(但反之亦然)。< / p>
语言中是否有任何令人讨厌的惊喜会让这个想法变得糟糕?