Struct Initialization Arguments

19,869

Solution 1

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; 
    }
}; 

Solution 2

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;
};

Solution 3

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 };

Solution 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.

Share:
19,869
Michael Hang
Author by

Michael Hang

Updated on June 08, 2022

Comments

  • Michael Hang
    Michael Hang almost 2 years

    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.

  • jogojapan
    jogojapan over 11 years
    To nitpick, another difference is that default inheritance is public for structs and private for classes.
  • jogojapan
    jogojapan over 11 years
    Why don't you use an initialization list in the constructor?
  • kevintodisco
    kevintodisco over 11 years
    @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?
  • ildjarn
    ildjarn over 11 years
    @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.
  • egrunin
    egrunin over 11 years
    Personally, I find having members default to private encourages good design.
  • kevintodisco
    kevintodisco over 11 years
    @ildjarn Ah, thanks. I had not considered the case of references.
  • Michael Hang
    Michael Hang over 11 years
    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.
  • egrunin
    egrunin over 11 years
    My reason may not be persuasive, but it is valid.
  • Michael Hang
    Michael Hang over 11 years
    My apologies: I pressed the enter key prematurely- I didn't mean to question the validity of your statement.
  • egrunin
    egrunin over 11 years
    Ah, understood. In any case your reasons for doing this are good: such a constructor makes the code more readable and less error-prone.
  • justin
    justin over 11 years
    @ildjarn i +1'ed your answer for demonstrating the next best alternative :)
  • ildjarn
    ildjarn over 11 years
    @egrunin : When the very first line of your class is public: then I'd argue that class is just noise. ;-]
  • egrunin
    egrunin over 11 years
    @ildjarn : if this were the entire class, I'd agree with you.
  • JamesTheAwesomeDude
    JamesTheAwesomeDude almost 6 years
    Just wondering: why the ampersands?
  • justin
    justin almost 6 years
    @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++
  • Vassilis
    Vassilis over 3 years
    For aggregate types, see these answers.