使用const_cast来排除getter是不是很糟糕?

时间:2014-12-25 10:28:37

标签: c++

在这种情况下使用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>

语言中是否有任何令人讨厌的惊喜会让这个想法变得糟糕?

0 个答案:

没有答案