Default parameter constructor with Member intialization lists?

Nov 15, 2015 at 11:15pm
Is there a way in c++ to combine a default parameter constructor with member initialization list?

So something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13

class Example{
 private:
 int var1;
 string var2;

 public:

 Example(int a = 0, string b = ""): var1(a), var2(b)
{ }

};
 


The intention here is that if nothing is passed in, a and b are initialized to 0 and empty string, respectively. If, however, an int and string are passed in, then var1 and var2 will be set to their values via member initialization.

Is this legal?
Last edited on Nov 15, 2015 at 11:16pm
Nov 16, 2015 at 3:14am
No replies?

Nov 16, 2015 at 3:16am
Yes, this is legal.
Nov 16, 2015 at 3:17am
You already have the code, why don't you just try it?
Nov 16, 2015 at 5:37am
Because im lazy :)


But thank you JLBorges for your response, which is now deleted for some reason.

For the record, I did try it, and it worked.

Topic archived. No new replies allowed.