mardi 25 octobre 2016

C++ pointer to object, can't read variables after 'return;'

In my Chamber class I set the nextChamber to a pointer that points to another Chamber when the user wants to move.

I do that with the following code:

nextChamber = &(layer->findChamber(x2, y2));

In Chamber.h I have:

Chamber* nextChamber;

with nextChamber being a Chamber* and layer being a DungeonLayer*. findChamber returns a Chamber object so that's why the &(). But I think something is going wrong here.

When I break on this point it sets the nextChamber variable nicely and I can see it's the right one (I use visual studio).

Proof: nextChamber is written normally

But after this it goes to the function above and return; that void function because the user is not allowed to make another choice because you are in a new Chamber and it has to initialize again.

nextChamber is altered to a nullstate?

The above happens to nextChamber but that's the only variable that changed, the others are still acting normally.

Can someone explain what is going on?

Code snippets:

//Function
    if (enemy != NULL) {
        std::cout << "flight" << std::endl;
        moveToChamber(); //this method contains the setNextChamber
        return; //after this, nextChamber is altered to the 'null'-state
    }
//endfunction


void Chamber::moveToChamber() {
    ........initialisation of the varialbles and a switch.........

    nextChamber = &(layer->findChamber(x2, y2));
    };

Aucun commentaire:

Enregistrer un commentaire