Kelevra
Kelevra

Reputation: 13

C++ Pointer to Object as Class member

I'm trying to get two different classes to interact with eachother, for that I have in one class a pointer to an object of an other class, which is specified in the constructor. Interaction works so far, I can change the paramters of the pointed-to object and I can see the changes, as I'm printing it on a terminal. BUT when I try to get a parameter from this object and try to print it to the terminal through the class which points to it I only get a zero value for an Int from which I know, cause of debug outputs, that it isn't zero, if called directly. I will give you an example of the code:

Class A:

class Spieler
{
    private:
        int score;
        Schlaeger *schlaeger;
        int adc_wert;
        int channel;


    public:

        Spieler(int x, Schlaeger &schl, int adc_wert_c=0, int channel_c=0 )
        {
            score=x;
            schlaeger=&schl;
            adc_wert=adc_wert_c;
            channel=channel_c;
        }
    //....
        void set_schl(Schlaeger &schl){ schlaeger=&schl;}
        int getPosY(){ schlaeger->getSchlaeger_pos_y();}
        int getPosX(){ schlaeger->getSchlaeger_pos_x();}
        void setPosY(int y){ schlaeger->set_pos_y(y);}      

        void schlaeger_zeichen(){
            schlaeger->schlaeger_zeichen();
        }

        void schlaeger_bewegen(){
            schlaeger->schlaeger_bewegen(getADC());
        }
//...

};

Class B:

class Schlaeger
{
    private:
        int schlaeger_pos_x;
        int schlaeger_hoehe;
        int schlaeger_pos_y;
    public:

        Schlaeger(int x=0, int h=5, int pos_y=15)
        {
            schlaeger_pos_x=x;
            schlaeger_hoehe=h;
            schlaeger_pos_y=pos_y;
        }

        int getSchlaeger_pos_x()
        {
            return schlaeger_pos_x;
        }
        int getSchlaeger_pos_y()
        {
            return schlaeger_pos_y;
        }
        int getSchlaeger_hoehe()
        {
            return schlaeger_hoehe;
        }
        void set_pos_y(int new_y)
        {
            schlaeger_pos_y=new_y;
        }


};

The calls to the changing methods work, I can see the changes and I can see it in a debug output.

Upvotes: 0

Views: 124

Answers (1)

DGomez
DGomez

Reputation: 1480

You're not returning the value in the getter

int getPosY(){ schlaeger->getSchlaeger_pos_y();}

should be

int getPosY(){ return schlaeger->getSchlaeger_pos_y();}

Upvotes: 4

Related Questions