5

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.

justin
  • 104,054
  • 14
  • 179
  • 226
Michael Hang
  • 199
  • 2
  • 2
  • 15

4 Answers4

8

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;
};
justin
  • 104,054
  • 14
  • 179
  • 226
  • 2
    @ildjarn i +1'ed your answer for demonstrating the next best alternative :) – justin Nov 20 '12 at 06:08
  • Just wondering: [why the ampersands](https://stackoverflow.com/a/4705871/1874170)? – JamesTheAwesomeDude May 29 '18 at 13:15
  • @JamesTheAwesomeDude in c++ the ampersands specify parameters will be passed by *reference*. whether you/somebody feels values or references are superior in this use case boils down to a matter of taste. as in the answer you linked, somebody could outline pros of references and not-so-obvious pitfalls in using values over references. a reference may be slower or faster, or optimized to have equivalent assembly. from a semantic perspective, how could a client screw this up when const references are used and optimizations are enabled? and finally the answer: because it is my convention for c++ – justin Jun 22 '18 at 15:52
8

Aside from giving your type a constructor, because as-shown it is an aggregate type, you can simply use aggregate initialization:

player p = { "name", 42 };
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • For *aggregate* types, see [these](https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special) answers. – Vassilis Aug 26 '20 at 13:05
5

Although (as has been pointed out), you can simply add a constructor:

struct player() {
    string name;
    int rating;
    player(string Name, int Rating) { 
        name = Name; rating = Rating; 
    }
}; 

Is there some reason you don't want to make it a class?

class player() {
public:
    string name;
    int rating;
    player(string Name, int Rating) { 
        name = Name; rating = Rating; 
    }
}; 
egrunin
  • 24,650
  • 8
  • 50
  • 93
  • 1
    Personally, I find having members default to private encourages good design. – egrunin Nov 20 '12 at 05:53
  • I'm not sure if this is a valid reason, but I simply wanted something for data encapsulation; in this case, it's not important that anything remains private since it's ultimately a piece of a larger class. – Michael Hang Nov 20 '12 at 06:03
  • My reason may not be *persuasive*, but it is *valid*. – egrunin Nov 20 '12 at 06:04
  • 1
    My apologies: I pressed the enter key prematurely- I didn't mean to question the validity of your statement. – Michael Hang Nov 20 '12 at 06:05
  • Ah, understood. In any case your reasons for doing this are good: such a constructor makes the code more readable and less error-prone. – egrunin Nov 20 '12 at 06:07
  • @egrunin : When the very first line of your class is `public:` then I'd argue that `class` is just noise. ;-] – ildjarn Nov 20 '12 at 07:15
  • @ildjarn : if this were the entire class, I'd agree with you. – egrunin Nov 20 '12 at 07:28
4

You are allowed to declare a constructor for your player struct:

struct player {
    string name;
    int rating;

    player(string n, int r) :
        name(n),
        rating(r)
    {
    }
};

In C++ one of the few differences between classes and structs is that class members default to private, while struct members default to public.

Community
  • 1
  • 1
kevintodisco
  • 5,061
  • 1
  • 22
  • 28
  • 1
    To nitpick, another difference is that default _inheritance_ is public for structs and private for classes. – jogojapan Nov 20 '12 at 05:48
  • 1
    Why don't you use an initialization list in the constructor? – jogojapan Nov 20 '12 at 05:49
  • 1
    @jogojapan Good to note, thanks for nitpicking :). Regarding initialization list: also an option. Stylistic choice on my part I guess. Is there an advantage to doing so? – kevintodisco Nov 20 '12 at 05:50
  • 1
    @ktodisco : One is initialization, the other is not (it's assignment). For semantic reasons this can be important (e.g. const/reference data members), and it's never worse with respect to performance. – ildjarn Nov 20 '12 at 05:52
  • @ildjarn Ah, thanks. I had not considered the case of references. – kevintodisco Nov 20 '12 at 05:54