加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 当一个方法只接受Foo * const时,是否应该const_cast“this

发布时间:2020-12-16 06:03:40 所属栏目:百科 来源:网络整理
导读:我有一个类Foo,它是一个自引用树状结构(最低限度): class Foo { public: // Gets this child's position relative to it's parent. int getPosition() const { return parent-indexOf(this); } int indexOf(const Foo *const child) const { return childre
我有一个类Foo,它是一个自引用树状结构(最低限度):
class Foo {
    public:
        // Gets this child's position relative to it's parent.
        int getPosition() const {
             return parent->indexOf(this);
        }

        int indexOf(const Foo *const child) const {
            return children.indexOf(child); // this line causes an error.
        }
    private:
        Foo *parent;
        QList<Foo *> children;
}

返回children.indexOf(child)的行返回const T和值按照QList docs传递,这解决了我的场景的Foo * const& value.

为了使我的getPosition()方法调用我自己的indexOf()方法,需要将const Foo * child的签名最小化,以便从const方法传递. (因为这是const Foo * const).

我的代码将不会编译为const Foo * const小孩不能被转换为Foo * const小孩为QList :: indexOf.我的任何一种方法都不会修改对象状态,所以它们应该是const(也就是说,我不想让getPosition不用来收到一个非const).

所以问题是,我如何从一个const上下文(const Foo * const)到QList :: indexOf所需要的.我应该把它放在getPosition中,因为我知道我的indexOf(和后面的调用)不会变异吗?

还有什么我应该做的吗也许我的设计是错误的.

解决方法

我认为这对于const_cast来说是一个完美合理的用例,但是这不是const_cast需要的,而是孩子.在这种情况下,QList :: indexOf期望一个指向Foo(Foo * const)的常量指针,但是child是一个常量指针,它指向常量Foo(const Foo * const).没有从Foo * const到const Foo * const的隐式转换,因为这将从指向的值中删除常量.

所以,要修复你的代码,我会改变行

return children.indexOf(const_cast<Foo*>(child));

你知道QList :: indexOf不会修改任何孩子指向,所以这不会产生未定义的行为.然而,我会添加一个解释为什么const_cast是必要的注释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读