Qt4 C++指向 const QList 指针的指针

发布于 2024-09-27 02:38:42 字数 1739 浏览 4 评论 0原文

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

#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 而不是 QList 时,没有这样的问题但我需要在代码中使用 QList

感谢您的帮助。

I got stuck with pointer to const QList of pointers to Foo. I pass pointer to myListOfFoo from Bar object to Qux. I use pointer to const to prevent making any changes outside Bar class. The problem is that I'm still able to modify ID_ executing setID in Qux::test().

#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();
}

Result of above code is:

-1
100

and what I'm trying to achieve is:

-1
no change
-1

There is no such problem when I use QList<Foo> instead of QList<Foo*> but I need to use QList<Foo*> in my code.

Thanks for help.

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

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

发布评论

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

评论(3

探春 2024-10-04 02:38:42

应该是:

QList<const Foo *>* listOfFoo;

should be:

QList<const Foo *>* listOfFoo;
旧人九事 2024-10-04 02:38:42

您可以使用 QList; const * 表示不允许修改列表或列表内容。问题是没有简单的方法从 QList 检索该列表,因此您需要将其添加到 Bar 类中。

You could use a QList<Foo const *> const * which means you are not allowed to modify the list or the content of the list. The problem is that there is no easy way to retrieve that list from a QList<Foo*>, so you need to add it in your Bar class.

永不分离 2024-10-04 02:38:42

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

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

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

QList<const Foo*> const* listOfFoo;

If you really have to return pointer, cast it to QList containing pointers to constant elements:

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

In Qux listOfFoo should contain pointers to constant elements too:

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