常数类成员,赋值运算符和QList [英] Constant class members, assignment operator and QList

查看:1460
本文介绍了常数类成员,赋值运算符和QList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请确认我是否正确,并告诉我是否有更好的解决方案:



我理解具有常量成员的对象 int const width; 不能由编译器隐式创建的合成赋值运算符处理。但QList(和我认为std :: list)也需要一个工作赋值运算符。因此,当我想使用具有常量成员和QList的对象时,有三种可能性:


  1. 不要使用常量成员。 (不是解决方案)

  2. 实施我自己的赋值运算符。

  3. 使用一些不需要赋值的容器
    operator

是否正确?还有其他优雅的解决方案吗?



也不知道我能否:




  • (4)强制编译器创建一个处理常量成员的赋值操作符! (我不明白为什么这是一个大问题,为什么操作员不够聪明,内部使用初始化列表或者我缺少一些东西?)

  • 我将不会在列表中使用赋值操作。



编辑:我从来没有分配过这个类的对象。它们仅由复制构造函数或重载的构造函数创建。所以赋值运算符只是容器而不是自己需要的。



EDIT2:这是我创建的赋值运算符。我不知道如果它的正确虽然。 Cell具有两个参数构造函数。这些参数使用初始化列表设置两个常量成员。但是该对象还包含其他变量(非const)成员。

  Cell :: operator =(Cell const& other)
{
if(this!=& other){
Cell * newCell = new Cell(other.column(),other.row ());
return * newCell;
}
return * this;
}



EDIT3:我发现这个线程几乎有同样的问题:C++:与const类成员的STL麻烦。所有的答案组合在一起回答了我的问题。



主要的问题是QList需要赋值操作符为因为他们内部使用赋值。因此,它们与接口混合实现。所以虽然你不需要赋值运算符QList将不工作没有它。 来源



@ 3.有std :: List,但它不提供对元素的常量时间访问,而QList则是。



@ 2.可以通过使用复制构造函数和所需的属性创建一个新对象并返回它*。虽然你绕过了const属性,它仍然比使用没有const,因为你会允许容器在这里欺骗,但仍然阻止用户自己做这是这个成员的原始意图,这是常数。



但是考虑到创建重载的赋值操作符会增加代码的复杂性,并且可能会引入比成员首先解决的常数更多的错误。



@ 1.最后这似乎是最简单的解决方案。只要它是私人的,你只需要注意,对象不会改变它本身。



@ 4.没有办法强迫他。他不知道如何因为变量是常量,在某些时候他必须用 int来做 this-> row = other.row const row; 之前定义。即使在这种情况下,const也是常数。 一个来源



@ 5 QList没有此类选项。



其他解决方案:




  • 使用指向对象而非纯对象的指针



现在不确定


Please conform if I am correct and tell me whether there is a better solution:

I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities:

  1. Don't use constant members. (Not a solution)
  2. Implement my own assignment operator.
  3. Use some other container that does not need assignment operators

Is that correct? Are there other elegant solutions?

Also I wonder whether I can:

  • (4) Force the compiler to create a assignment operator that deals with constant members! (I don't understand why this is such a big problem. Why is the operator not intelligent enough to use initialization lists internally? Or am I missing something?)
  • (5) Tell QList that I will never use assignment operations in the list.

EDIT: I never assign objects of this class myself. They are only created by the copy constructor or by an overloaded constructor. So the assignment operator is only required by the container not by myself.

EDIT2: This is the assignment operator I created. I am not sure if its correct though. Cell has a two parameter constructor. These parameters set the two constant members with initialization lists. But the object also contains other variable (non const) members.

Cell& Cell::operator=(Cell const& other)
{
 if (this != &other) {
  Cell* newCell = new Cell(other.column(), other.row());
  return *newCell;
 }
 return *this;
}

EDIT3: I found this thread with almost the same question: C++: STL troubles with const class members. All answers combined together answered my questions.

解决方案

I'll try to bundle the answers in short:

The main problem is that QList requires the assignment operator to be present because they internally use assignment. Thus they mix implementation with interface. So although YOU don't need the assignment operator QList won't work without it. source

@ 3. There is std::List but it doesn't offer constant time access to elements, while QList does.

@ 2. It is possible by creating a new object with the copy constructor and the desired properties and returning it*. Although you circumvent the const property it is still better than using no const at all because you would allow the container to cheat here but still prevent users to do this themselves which was the original intention of making this member constant.

But take into account that creating an overloaded assignment operator adds to the complexity of the code and might introduce more errors than the const-ing of the members would solve in the first place.

@ 1. In the end this seems to be the easiest solution. As long as it's private you just have to pay attention that the object doesn't change it itself.

@ 4. No way to force him. He wouldn't know how because the variable is constant and at some point he would have to do this->row = other.row with int const row; previously defined. And const means constant even in this case. one source

@ 5 QList has no options of this kind.

Additional solutions:

  • Use pointer to objects instead of pure objects

*Not sure about this at the moment.

这篇关于常数类成员,赋值运算符和QList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆