views:

103

answers:

3

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.

+1  A: 

should be:

QList<const Foo *>* listOfFoo;
noisy
If I do it I need to change `QList<Foo*> *` into `QList<const Foo *>*` in other lines to avoid compilation error: cannot convert 'const QList<const Foo*>*' to 'const QList<Foo*>*' in assignment. But then I can't perform setID from any place (e.q. Bar::Bar())
Moomin
yes, you can ;)const_cast<Foo*>(this->listOfFoo->last())->setID(100);
noisy
+1  A: 

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.

tibur
A: 

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;
Valentin Heinitz
Acctually I think there might be something wrong with your solution because after inputing "QList<Foo*> const * getMyListOfFoo() {return reinterpret_cast <QList <Foo*> *>(myListOfFoo_);}" there is no change in results - still -1 and 100
Moomin
If you could explain your idea with giving more details maybe I would understand it. Thanks in advance.
Moomin
Sorry for late response and some formating errors. Now it should work. The idea is, the elements in list should be constant as suggested by Frank on Oct 12 at 13:52. Reinterprete_cast helps you to convert QList<Foo*> used inside the class to QList<const Foo*> for outside-use.
Valentin Heinitz