Reputation: 809
I have this setup and how i want to use it in main for example:
struct Player{
private:
float posX1, posX2, posY1, posY2;
int user_id;
public:
Player::Player():posX1(NULL), posY1(NULL), posX2(NULL), posY2(NULL){};
float getPosX1();
float getPosY1();
float getPosX2();
float getPosY2();
int getUserId();
void setUserId(int);
void setPosX1(float);
void setPosY1(float);
void setPosX2(float);
void setPosY2(float);
};
struct Room_Data{
private:
int room_id;
Player player_one, player_two;
bool playerOneAlive, playerTwoAlive;
public:
Room_Data::Room_Data():player_one(), player_two(), playerOneAlive(false), playerTwoAlive(false), room_id(NULL){};
void setRoomId(int);
int getRoomId();
void setPlayerOneDead();
void setPlayerOneAlive();
void setPlayerTwoDead();
void setPlayerTwoAlive();
bool isPlayerOneAlive();
bool isPlayerTwoAlive();
Player getPlayerOne();
Player getPlayerTwo();
};
I'm trying to use the following method in main for example:
main.cpp
const int x=10;
Room_Data data[x];
data[0].getPlayerOne().setPosX1(10.0f);
But it doesnt set posx1 because getPlayerOne() provides only private access of the player_one object but i dont know how to give it public access
Upvotes: 1
Views: 57
Reputation: 385395
By having getPlayerOne
return a reference to that private variable.
Player& getPlayerOne();
Then you will be able to perform stuff on the result of calling that function, and that stuff will apply to the actual, original member within your class.
Right now you're operating on a temporary copy, instead, due to returning by value.
Frankly, though, if you're going to allow unabashed access to this private variable, why bother with the getter? Just make the variable public.
Upvotes: 3