if i want a class constructor to be able to take multiple default member arguments am i really forced to create a constructor for each? it seems very tedious when i have say 5-6 member variables that i want to be able to be initialized via the constructor.
is there some way i can just have one constructor that just "accepts" a list of any amount of arguments and then i specify which argument (if passed) should be used to initialize a member variable. Something like this:
and yes i realize two of the constructors are ambiguous that was just a mistake
Elijah Watson
The third parameter in the one on the bottom could accept an empty string as the default argument. Also, stop using const value parameters and member variables. It's completely pointless 99% of the time
>The third parameter in the one on the bottom could accept an empty string as the default argument i dont want the user to think they are forced to specify args they aren't. They might not know what to put there
>Also, stop using const value parameters and member variables why? what if someone passes a flag object as a const? if i dont mark getters functions & the corresponding member variables as const, then they cant be called on that object
Asher Phillips
>i dont want the user to think they are forced to specify args they aren't. They might not know what to put there nvm i see what u mean now
>why? what if someone passes a flag object as a const? It's irrelevant whether the actual parameter is const or not. It's also irrelevant whether the formal parameter is const std::string or std::string. The only difference between the two is that you are going to operate on a const std::string in the implementation of your function, it doesn't matter at all for its interface and objects that are used as actual parameters
>if i dont mark getters functions & the corresponding member variables as const, then they cant be called on that object ...What?
Brandon Ramirez
i see, thanks user
Mason Bennett
std::optional and cool it with consts
James Richardson
Make an Arg struct struct Arg { int arg1, arg2, arg3, arg4, arg5 }
>cool it with consts Everything that can be const should be const. What OP needs to cool it with is PASSING STRINGS BY FUCKING VALUE.
Isaac Sanchez
>Everything that can be const should be const Why
Daniel King
>fags help >class named flag >8 consturctors for the same thing >virtual destructor >shitty colorscheme >CPL exceeded >screenshot has chopped line numbers my kind of art, right here
Sebastian Carter
This is the basic, but the proper way is to understand wtf are you wanting 8 flags? Are they all the same thing? Or derivatives of a single thing? Dog inherits animal Cat inherit animal Kitten inherits Cat, default age=1, etc etc
Without specifics it’s hard to give an exact answer
Alexander Gutierrez
Here are two options. Either use the GNU struct extension that let's you construct object inline. using std::string; struct Flag { const string m_name; const string m_alias; const string m_desc;
bool m_set = false; }
...
Flag flag = { .m_set = true .m_alias = "Alias"; .m_name = "My name"; };
Or use an unordered map or a fixed vector of pairs (the standard library does not have that iirc) for out of order construction struct Flag { using args = unordered_map; Flag(string_view name = {}, string_view alias = {}, string_view desc= {}, bool set = {}) :m_name(name), m_alias(alias), m_desc(desc), m_set(set) {} Flag(bool set, string_view name = {}, string_view alias = {}, string_view desc= {}) :Flag(name, alias, desc, set) {} // Constructor for out of order. Flag(const args& arg, bool set = {}) :m_set(set) { if(arg.contains("name")) m_name = arg["name"]; if(arg.contains("alias")) m_name = arg["alias"]; if(arg.contains("desc")) m_name = arg["desc"]; } Flag(bool set, const args& arg) :m_set(set) {}
string m_name; string m_alias; string m_desc;
bool m_set = false; }
...
Flag flag1 = {"name", "alias", "desc", true}; Flag flag2 = {{"alias", "123", "name", "2"}, false}; Flag flag3 = {{"desc", "aaa", "alias", "bbb"}, false};
Christian Hernandez
Three typos. Should be Flag flag2 = {{"alias", "123"}, {"name", "2"}}, false};
I accidentally put name in every field of the constructor. The final constructor should just be Flag(bool set, const args& arg) :Flag(arg, set) {}