指向委托给removeAll()的方法的常量指针参数

发布于 2024-09-16 18:05:30 字数 361 浏览 2 评论 0原文

考虑这样的方法:

void Parent::removeChild(Child *child)
{
    children.removeAll(child);
}

在这种情况下,由于 child 本身永远不会被修改,因此可以将其设为常量指针。但由于 children 的类型为 QList,removeAll() 采用对非 const 指针的 const 引用。

建议的处理方法是什么?跳过方法参数的 constness 或 const_cast const 指针以适合 removeAll() 方法?

Consider a method like this:

void Parent::removeChild(Child *child)
{
    children.removeAll(child);
}

In this case, since child is never modified itself, one could make it a const pointer. But since children is of the type QList, the removeAll() takes a const reference to a non-const pointer.

What's the recommended way to handle this? Skip the constness of the method argument or to const_cast the const pointer to fit the removeAll() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-09-23 18:05:30

棘手的一个。您应该添加更多代码,但是从 docs 我认为您有一个 QList 并且无法将其更改为 QList 因为您需要访问非常量中的实际对象方式。

由于 removeAll() 函数所做的只是删除列表中的条目,并且它绝不会修改所指向的 Child (怎么可能呢,它不知道有关 Child 类的任何信息)在这里使用 const_cast 是安全的。

Tricky one. You should have added some more code, but from the docs I assume that you have a QList<Child*> and cannot change it to a QList<const Child*> because you need to access the actual objects in a non-const manner.

Since all the removeAll() function does is to remove the entry in the list and it in no way modifies the pointed-to Child (how could it, it doesn't know anything about the Child class) it would be safe here to use a const_cast.

醉态萌生 2024-09-23 18:05:30

如果您想说的是更改子列表不是更改父实例,那么只需将子列表设为 可变

但是,请确保它确实是您想要的父类语义。如果子级不是父级状态的一部分(在系统的语义中),那么应该没问题。如果它是父实例状态的一部分,那么您应该保留一个非常量成员函数。

mutable 关键字适用于那些特殊情况,其中类的成员不应被视为实例的状态,而只是附加信息。如果这是您的情况,请使用它。

否则,让您的成员函数成为非常量函数,因为它会修改父成员。

If what you want is to say that changing the children list is not a change of a Parent instance, then just make the children list mutable.

However, make sure that it's really the semantic you want for you Parent class. If children are not part of the state of Parent (in your system's semantic), then it should be fine. If it is part of the Parent instance state, then you should keep a non-const member function.

The mutable keyword is there for thoses exceptional cases where a member of a class shouldn't be taken as the state of an instance, just additional informations. If it's your case then use it.

Otherwise let your member function be non-const as it modifiesa Parent member.

安静 2024-09-23 18:05:30

看起来 QList 是设计用于非指针的。他们将很多 const 接口定义为 const T&,如果您的 QList 位于 Child 上,而不是 上,那么这会非常有用。孩子*

它可以很好地处理指针,但它不能声明适合它们的常量。我不建议将您的 QList 更改为 Child ,除非复制起来很便宜,您拥有复制 ctor、dtor、op=、op== 等的所有正确语义,并且您不介意在列表中拥有副本而不是在您传入的对象。您可以看到如何使用 int 或字符串,它会按预期工作(removeAll 将是 const 正确的)。

如果 const 正确性对您很重要,那么使用 const_cast。然后声明一个 const ref 并将其传入。

void Parent::removeChild(const Child *child)
{
    QList<Child*>::const_reference constRefToChild = const_cast<Child *> child;
    children.removeAll(constRefToChild);
}

这样做的要点是,如果将removeAll 更改为不采用 const,则会出现编译器错误。然后您就会知道removeAll 并没有保留参数的常量性。

It looks like QList is designed to be used on non-pointers. They define a lot of the interface that is const as const T& which would work great if your QList was on Child, not Child*.

It will work on pointers just fine, but it can't declare the constness right for them. I don't recommend changing your QList to Child unless it's cheap to copy, you have all the right semantics for copy ctor, dtor, op=, op==, etc and you don't mind having copies in the list rather than the objects you pass in. You can see how with int or strings, that it would work as expected (removeAll would be const correct).

If const correctness is important to you, then use a const_cast. Then declare a const ref and pass that in.

void Parent::removeChild(const Child *child)
{
    QList<Child*>::const_reference constRefToChild = const_cast<Child *> child;
    children.removeAll(constRefToChild);
}

The point of this is that if removeAll is ever changed to not take a const, you get a compiler error. Then you'd know that removeAll isn't preserving constness of the argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文