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