【问题标题】:Qt4 C++ Pointer to const QList of pointersQt4 C++ Pointer to const QList of pointers
【发布时间】:2010-10-12 12:50:22
【问题描述】:

我被指向 const QList of pointers to Foo 的指针卡住了。我将指向myListOfFoo 的指针从Bar 对象传递给Qux。我使用指向 const 的指针来防止在 Bar 类之外进行任何更改。问题是我仍然可以修改ID_Qux::test() 中执行setID

#include <QtCore/QCoreApplication>
#include <QList>
#include <iostream>

using namespace std;

class Foo
{
private:
    int      ID_;
public:
    Foo(){ID_ = -1; };
    void setID(int ID) {ID_ = ID; };
    int  getID() const {return ID_; };
    void setID(int ID) const {cout << "no change" << endl; };
};

class Bar
{
private:
    QList<Foo*>  *myListOfFoo_;
public:
    Bar();
    QList<Foo*> const * getMyListOfFoo() {return myListOfFoo_;};
};

Bar::Bar()
{
    this->myListOfFoo_ = new QList<Foo*>;
    this->myListOfFoo_->append(new Foo);
}

class Qux
{
private:
    Bar *myBar_;
    QList<Foo*> const* listOfFoo;
public:
    Qux() {myBar_ = new Bar;};
    void test();
};

void Qux::test()
{
    this->listOfFoo = this->myBar_->getMyListOfFoo();
    cout << this->listOfFoo->last()->getID() << endl;
    this->listOfFoo->last()->setID(100); //           **<---- MY PROBLEM**
    cout << this->listOfFoo->last()->getID() << endl;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Qux myQux;
    myQux.test();

    return a.exec();
}

以上代码的结果是:

-1
100

而我想要实现的是:

-1
no change
-1

当我使用QList&lt;Foo&gt; 而不是QList&lt;Foo*&gt; 时没有这样的问题,但我需要在我的代码中使用QList&lt;Foo*&gt;

感谢您的帮助。

【问题讨论】:

  • QList const* - 不要在堆上创建 Qt 容器,它们是隐式共享的(写时复制)。只需通过 value/const 引用传递它们。
  • @Frank 感谢您的建议,但您能否详细说明如何操作。恐怕我的编程能力不够强,无法理解你的想法:)。
  • 如果你想从你的内部 QList 中获得一个 QList,你所能做的就是创建一个新列表并手动附加指针。 QList list() const { QList cl; /* 循环/追加... */ return cl; }。或者保留多个列表。
  • @Moomin:使成员成为普通的 QList 和 QList getMyListOfFoo() const 如上所述返回一个副本(我使用了 list())。那么外面没有人可以直接修改列表(因为他将在副本上操作),但这似乎是你想要的吗?
  • @Frank 我想就是这样,但我希望不必复制整个列表 - 它计划很长,因此它可能是非常消耗内存的解决方案。

标签: c++ qt qt4 const-correctness pointer-to-member


【解决方案1】:

应该是:

QList<const Foo *>* listOfFoo;

【讨论】:

  • 如果我这样做,我需要在其他行中将 QList&lt;Foo*&gt; * 更改为 QList&lt;const Foo *&gt;* 以避免编译错误:无法将 'const QList*' 转换为 'const QList*' 在分配中。但是我无法从任何地方执行 setID(例如 Bar::Bar())
  • 是的,你可以;) const_cast(this->listOfFoo->last())->setID(100);
【解决方案2】:

您可以使用QList&lt;Foo const *&gt; const *,这意味着您不能修改列表或列表的内容。问题是没有简单的方法从QList&lt;Foo*&gt; 中检索该列表,因此您需要将其添加到您的Bar 类中。

【讨论】:

    【解决方案3】:

    如果你真的必须返回指针,请将其转换为包含指向常量元素的指针的 QList:

    QList<const Foo*> const* getMyListOfFoo() 
    {return reinterpret_cast<QList<const Foo*> *>(myListOfFoo_);};
    

    在 Qux listOfFoo 中也应该包含指向常量元素的指针:

    QList<const Foo*> const* listOfFoo;
    

    【讨论】:

    • 实际上我认为您的解决方案可能有问题,因为在输入“QList const * getMyListOfFoo() {return reinterpret_cast *>(myListOfFoo_);}”结果没有变化 - 仍然是 -1 和 100
    • 如果你能通过提供更多细节来解释你的想法,也许我会理解它。提前致谢。
    • 对于迟到的回复和一些格式错误,我们深表歉意。现在它应该可以工作了。这个想法是,列表中的元素应该按照 Frank 在 10 月 12 日 13:52 的建议保持不变。 Reinterprete_cast 帮助您将类内部使用的 QList 转换为 QList 以供外部使用。
    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-05-09
    相关资源
    最近更新 更多