One Class Accessing Data of Another

I have two classes, Player and Wall, that each of a SDL_Rect data member. I need to access the SDL_Rect in Wall from a function in my Player class in order to check for collision.

Here are some code snippets (NOTE: They are in the same header file):

Wall
1
2
3
4
5
6
7
8
9
10
11
12
13
class Wall
{
    public:
        Wall(int x, int y, int w, int h, std::string filename);
        ~Wall();

        SDL_Rect wall_outline;

        void show();

    private:
        SDL_Surface *wall;
};


Player
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Player
{
    public:
        Player(int x, int y, int w, int h, std::string filename);
        ~Player();

        SDL_Rect player_outline;

        void show();
        void handle_input();
        void move();

    private:
        int its_x, its_y;
        int xVel, yVel;
        int SPEED;
        SDL_Surface *player;
};


Player::move
1
2
3
4
5
6
7
8
9
10
11
void Player::move()
{
    its_x += xVel;

    if (its_x < 0 || its_x + 30 > SCREEN_WIDTH || check_collision(player_outline, wall_outline))
        its_x -= xVel;

    its_y += yVel;
    if (its_y < 0 || its_y + 70 > SCREEN_HEIGHT || check_collision(player_outline, wall_outline))
        its_y -= yVel;
}



Player::move can't access wall_outline. What can I do to allow it? Thanks in advance.
Last edited on
Do you even have a wall instance that you need to access?

But it's like any other class, you just do:

the_wall.wall_outline
Which wall does the wall_outline belong to?

wall_outline is not a member of Player, so you need to access it using either the . or -> operator with the relevant wall variable. e.g.

current_wall.wall_outline

current_wall_ptr->wall_outline

Andy
It actually wasn't resolved with such a simple solution. I had to do some file shuffling but got it to work.

Thanks for giving me a push.
Topic archived. No new replies allowed.