mercredi 22 janvier 2020

Passing class by reference doesn't change original

I have a class that handles rendering sprites, which has a Ball and Gun objects on it, sets their sprites, and renders them every frame. I want to pass in that ball object to gun, so that gun can fire when a user presses space (input is handled on gun). I tried pointers, and now passing by reference, but when I change it after passing it in it doesn't affect the original. In my main class I call gun.init(&ball) that I'll show here. Gun has a variable Ball mBall.

class Gun : public GameObj
{
public:
    Gun()
        :GameObj(), mWaitSecs(0)
    {}
    void Update();      
  void Init(Ball &ball);      //init function that passes in ball?
private:
    float mWaitSecs;    //delay after firing beforeo you can move/fire
    Ball mBall;
};

void Gun::Init(Ball &ball) {  
  mBall = ball;
}

Aucun commentaire:

Enregistrer un commentaire