Qt4 C++指向 const QList 指针的指针
我被指向 const QList ofpoints to Foo
的指针卡住了。我将 Bar
对象中指向 myListOfFoo
的指针传递给 Qux
。我使用指向 const 的指针来防止在 Bar
类之外进行任何更改。问题是我仍然可以修改在 Qux::test()
中执行 setID
的 ID_
。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该是:
should be:
您可以使用
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 aQList<Foo*>
, so you need to add it in yourBar
class.如果你确实必须返回指针,请将其转换为包含指向常量元素的指针的 QList:
在 Qux 中,listOfFoo 也应该包含指向常量元素的指针:
If you really have to return pointer, cast it to QList containing pointers to constant elements:
In Qux listOfFoo should contain pointers to constant elements too: