Default parameter constructor with Member intialization lists?

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
No replies?

Yes, this is legal.
You already have the code, why don't you just try it?
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.