初始化列表类

时间:2017-12-02 22:58:23

标签: c++

大家好我正在尝试为我想制作的游戏制作一个代码现在我有点卡在类上并希望使用列表初始化来更容易进入玩家属性这里是我的班级下面这是我的主要代码的一部分,因为我不想制作标题或另一个.cpp文件。

class Player {

public:

string name;
int id;
int height;
double closeshot;
double midshot;
double threeshot;
double dribble;
double defense;
double jump;

Player() {};
Player(string a, int b, int c, double d, double e, double f, double g, double h, double i);


}pg1,pg2,sg1,sg2;


Player::Player(string a, int b, int c, double d, double e, double f, double g, double h, double i) {

    name = a;
    id = b;
    height = c;
    closeshot = d;
    midshot = e;
    threeshot = f;
    dribble = g;
    defense = h;
    jump = i;

}

以下是我的播放器初始化列表。

Player pg1( "Player1Home", 1, 61, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );
Player pg2( "Player1Away", 6, 72, 10.00, 10.00, 10.00, 10.00, 10.00, 15.00 );
Player sg1( "Player2Home", 2, 61, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );
Player sg2( "Player2Away", 7, 71, 10.00, 10.00, 10.00, 10.00, 10.00, 10.00 );

任何帮助都会感激,我还是比较新的。

所以我决定回到结构,因为它们更容易使用

struct Player {

string name;
int id;
int height;
double closeshot;
double midshot;
double threeshot;
double dribble;
double defense;
double jump;


}pg1,pg2,sg1,sg2;

这就是我在我的价值观中添加的方式

        pg1.name = "Player1Home";
        pg1.id = 1;
        pg1.height = 61;
        pg1.closeshot = 10.00;
        pg1.midshot = 10.00;
        pg1.threeshot = 10.00;
        pg1.dribble = 10.00;
        pg1.defense = 10.00;
        pg1.jump = 10.00;

        pg2.name = "Player1Away";
        pg2.id = 6;
        pg2.height = 61;
        pg2.closeshot = 10.00;
        pg2.midshot = 10.00;
        pg2.threeshot = 10.00;
        pg2.dribble = 10.00;
        pg2.defense = 10.00;
        pg2.jump = 10.00;

        sg1.name = "Player2Home";
        sg1.id = 2;
        sg1.height = 61;
        sg1.closeshot = 10.00;
        sg1.midshot = 10.00;
        sg1.threeshot = 10.00;
        sg1.dribble = 10.00;
        sg1.defense = 10.00;
        sg1.jump = 10.00;

        sg2.name = "Player2Away";
        sg2.id = 7;
        sg2.height = 61;
        sg2.closeshot = 10.00;
        sg2.midshot = 10.00;
        sg2.threeshot = 10.00;
        sg2.dribble = 10.00;
        sg2.defense = 10.00;
        sg2.jump = 10.00;

当我以这种方式离开时,我的其余代码效果会更好。

0 个答案:

没有答案