Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct Initialization Arguments

I have a struct, player, which is as follows:

struct player {
string name;
int rating;
};

I'd like to modify it such that I declare the struct with two arguments:

player(string, int)

it assigns the struct's contents with those values.

like image 521
Michael Hang Avatar asked Nov 29 '22 08:11

Michael Hang


1 Answers

you would use the constructor, like so:

struct player {

  player(const string& pName, const int& pRating) :
    name(pName), rating(pRating) {} // << initialize your members
                                    //    in the initialization list

  string name;
  int rating;
};
like image 197
justin Avatar answered Dec 05 '22 09:12

justin