Qt变量重新赋值 [英] Qt variable re-assignment

查看:1574
本文介绍了Qt变量重新赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个例子我有一个问题。让我通过一些代码解释:

I have two examples I have a question about. Let me explain via some code:

问题1:

QStringList qsl(); // Create a list and store something in it
qsl << "foo";
QString test = "this is a test"; 
qsl = test.split(" ", QString::SkipEmptyParts); // Memory Leak?

当我重新分配qsl变量对foo发生什么以及原始数据分配时会发生什么第一行?

What happens when I re-assign the qsl variable what happens to "foo" and the original data allocated on the first line?

问题2:

class Foo
{
     QStringList mylist;
     void MyFunc(QStringList& mylist)
     {
           this->m_mylist = mylist;

     }

     void AddString(QString str)
     {
         mylist << str;
     }



}

int main()
{
    Foo f;
    QStringList *qsl = new QStringList();
    f.MyFunc(*qsl);
    delete qsl;
    f.AddString("this is a test"); // Segfault?
}

这里我通过引用传递一个列表,在所述类中。然后我删除原始对象。

Here I'm passing a list by reference to a class which is then stored in said class. I then delete the original object.

这基本上都是当你分配QObject到QObject时会发生什么。我假定已经创建了一个对象的副本,即使该对象是通过引用传递的(当然不是通过指针,这只是一个指针副本)。

It basically all comes down to what happens when you assign a QObject to a QObject. I assume a copy of the object is made, even if the object was passed in via reference (not via pointer of course, that would just be a pointer copy).

我还假设像QStringList的东西执行deepcopy ...是正确的吗?

I also assume that something like QStringList performs a deepcopy...is this correct?

推荐答案

分配到 QStringList 变量的作用与分配给C ++中的任何其他变量相同。对于对象,调用左侧对象的赋值运算符将右侧对象的内容复制到左侧的对象中。通常这只是一个成员赋值:

Assigning to a QStringList variable works the same as assigning to any other variable in C++. For objects, the assignment operator of the object on the left is called to copy the content of the object on the right into the object on the left. Usually this does just a memberwise assignment:

struct A {
  int x;
  QString y;

  A& operator=(const A &other) {
    // do the assignment:
    x = other.x;
    y = other.y;

    return *this;
  }
};

作业左边的对象自适应包含与对象相同的东西正确的。没有新的对象分配,只是现有的对象被修改。

The object on the left of the assignment "adapts itself" to contain the same things as the object on the right. There is no new object allocated, just the existing one is modified.

如果类更复杂,例如包含指针动态分配的数据 QStringList 的情况),赋值运算符可能会更复杂的实现。但这是 QStringList 类的实现细节,你不必担心。 QStringList 对象左侧的对象将被修改为等于右侧的对象。

If the class is more complicated and for example contains pointers to dynamically allocated data (like it is probably is the case for QStringList), the assignment operator might be more complicated to implement. But this is an implementation detail of the QStringList class and you should not have to worry about that. The QStringList object on the left of the assignment will be modified to be equal to the object on the right.

在问题2中,您将一个对象分配给一个成员变量,这会导致成员变量中的对象被修改,使其包含与分配给它的对象相同的东西。

In Question 2 you assign an object to a member variable, which causes the object in the member variable to be modified so that it contains the same things as the object that is assigned to it. That this other object later is deleted doesn't matter to the member variable.

在语义上,这与分配简单整数时相同:

Semantically this is the same as when assigning simple integers:

int i, j;
i = j;

修改存储 i ,因此它包含与 j 相同的值。稍后 j 会发生什么与 i 的值无关。

The memory where i is stored is modified, so that it contains the same value as j. What happens to j later on doesn't matter to the value of i.

这篇关于Qt变量重新赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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