'My pointer does not point on the same value when its call in a slot QT C++

I do a pokemon project with a Qt interface.

I created a class Trainer and Pokemon :


    class Pokemon
    {
    protected:
        string itsName;
        double itsHP;
    
    public:
        Pokemon(string name, double hp);
        virtual ~Pokemon();
        virtual void attack(Pokemon * pokemon) = 0;
        ...
    };


    class Trainer
    {
        string itsName;
        int itsLevel;
    
        vector<Pokemon *> * itsPokemons;
        Pokemon * itsCurrentPokemon;
    
    public:
        Trainer(string name, int level = 0);
        ~Trainer();
        ...
        vector<Pokemon *> * getItsPokemons() const;

    };


    vector<Pokemon *> *Trainer::getItsPokemons() const
    {
        return itsPokemons;
    }


There is the definition of my widget :


    class mainWidget : public QWidget
    {
        Q_OBJECT
    public:
        mainWidget(Trainer * player, Trainer * opponent, QWidget *parent = nullptr);
        ~mainWidget();
    
    
    private slots:
        void onComboBoxChanged(const QString str);
    
    private:
        Ui::Widget *ui;
        Trainer * itsPlayer;
        Trainer * itsOpponent;
    };


mainWidget::mainWidget(Trainer * player, Trainer * opponent,QWidget *parent)
    : QWidget{parent},
      ui(new Ui::Widget),
      itsPlayer(player),
      itsOpponent(opponent)
{
    ui->setupUi(this);

    ui->comboBox->addItem("test1");
    ui->comboBox->addItem("test2");

    cout << "Adress : " << itsPlayer->getItsPokemons() << endl;

    cout << "Vector size : " << itsPlayer->getItsPokemons()->size() << endl;

    connect(ui->comboBox,SIGNAL(currentTextChanged(const QString)), this, SLOT(onComboBoxChanged(const QString)));
}


    mainWidget::~mainWidget()
    {
        delete ui;
    }

In my widget, the QComboBox is connected with this method (its a slot):


    void mainWidget::onComboBoxChanged(const QString str)
    {
        cout << "Adress : " << itsPlayer->getItsPokemons() << endl;

        cout << "Vector size : " << itsPlayer->getItsPokemons()->size() << endl;    
    }

When I execute the code, the cout in the constructor print :

Vector size : 6 // the normal size of my vector
Adress : 0x2536e30

And when I use the QComboBox the cout in the slot print :

Adress : 0x2536e30
Vector size : 18446726501852669075

It seems that in the method onComboBoxChanged the pointer suddenly point on something random.


UPDATE : My widget was created as a pointer, but i did not put the key word "explicit" behind the constructor of this one. Its finally work



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source