Returning a pointer to an encapsulated data member

So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation. Any way to get around this?

Find my code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef PLAYER_H
#define PLAYER_H

#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"

class Player
{
public:
	Player();
	~Player();

	void setRow(int suppliedRow);
	void setColumn(int suppliedColumn);
	void setScore(int suppliedScore);
	void setLives(int suppliedScore);
	void setImg(SDL_Renderer* renderer, const char* filename);

	int getRow();
	int getColumn();
	int getScore();
	int getLives();
	SDL_Texture* getImg();

private:
	int row;
	int column;
	int score;
	int lives;
	SDL_Texture* img;
};

#endif 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Player.h"

Player::Player()
{
	img = nullptr;
	setRow(0);
	setColumn(0);
	setScore(0);
}

Player::~Player()
{
	SDL_DestroyTexture(img);
}

void Player::setRow(int suppliedRow)
{
	row = suppliedRow;
}
void Player::setColumn(int suppliedColumn)
{
	column = suppliedColumn;
}

void Player::setScore(int suppliedScore)
{
	score = suppliedScore;
}

void Player::setLives(int suppliedLives)
{
	lives = suppliedLives;
}

void Player::setImg(SDL_Renderer* renderer, const char* fileName)
{
	img = IMG_LoadTexture(renderer, fileName);
}

int Player::getRow()
{
	return row;
}

int Player::getColumn()
{
	return column;
}

int Player::getScore()
{
	return score;
}

int Player::getLives()
{
	return lives;
}

SDL_Texture* Player::getImg() // bad practice
{
	return img;
}
Last edited on
Normally its OK if you just make it const-correct, then they can't modify your image without setting it. Get/Set functions should be avoided if possible, normally - encapsulate them within a function, though in this case it makes sense. Change your definition to be similar to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Player {
    public:
        Player();
        ~Player();

        void setRow(int suppliedRow);
        void setColumn(int suppliedColumn);
        void setScore(int suppliedScore);
        void setLives(int suppliedScore);
        void setImg(SDL_Renderer* renderer, const char* filename);

        int getRow() const;
        int getColumn() const;
        int getScore() const;
        int getLives() const;
        const SDL_Texture* getImg() const;

    private:
        int row;
        int column;
        int score;
        int lives;
        SDL_Texture* img;
};
Topic archived. No new replies allowed.