Question about Classes

So I am making a Multiplayer Game with Classes, I have my Class player. And I want to call the instances Player.PLAYERNAME. Like for example a Player is called John and I want to access his stats, I would write:

class player {
private:
int Stats;
};

string PLAYERNAME = "John";
player Player.PLAYERNAME;
if (Player.John.Stats < 4) {
print "John sucks at this";
};

How do I do that / Is that even possible to do?.

Thanks for every answer.
name is a part of the players identity, so put it inside the class as a data member and as your class members are private you'll need appropriate getters. something on the following lines:
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
#include<iostream>
using namespace std;

class player
{
    private:
        string name;
        int stat;


    public:
        player(string _name, int _stat) : name(_name), stat (_stat){}
        string getName()const {return name;}
        int getStat()const {return stat;}
};
void player_eval(const player& p)
{
    if(p.getStat() < 4)
    {
        cout << p.getName() << " sucks at this! \n";
    }
    if (p.getStat () > 8)
    {
        cout << p.getName() << " is a STAR!!!! \n";
    }
}
int main()
{
    player p1("John", 3);
    player p2 ("James", 9);

    player_eval (p1);
    player_eval (p2);
}
Problem is, Amount of Players can differ and if i understood this correctly, I can't change the instance name. So how can I continue this ( p1 , p2 , p3 .. p16 ) and that only for the amount of players online on the server.
do you understand that if John is not a player he'll not have a data-member called stats?
It sounds like you want a number of players that can be identified (or "indexed") by a string, which is its name. You can do this with a map.

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
#include <map>

class player
{
public:
    player(int val) : Stats(val) {}
private:
    int Stats;
};

std::map<std::string, player> Player;

...

// Put the "John" player into the map
Player.insert("John, player(1);


...

string PLAYERNAME = "John";

//  Note: not quite the syntax you want, but close
if (Player[PLAYERNAME].Stats < 4) {
    print "John sucks at this";
}
 
That is actually perfect. Thanks alot @doug4.
Topic archived. No new replies allowed.