For example, a class named Table, with its constructor being:
Table(string name="", vector <string> mods);
How would I initialize the vector to be empty?
Edit: Forgot to mention this was C++.
Yes, a constructor can contain default argument with default values for an object.
By default, the size of the vector automatically changes when appending elements. To initialize the map with a random default value, below is the approach: Approach: Declare a vector. Set the size of the vector to the user defined size N.
The default constructor with argument has a default parameter x, which has been assigned a value of 0.
Table(string name="", vector <string> mods);
if you want vector to be empty inside constructor then
mods.clear();
or
mods.swap(vector<string>());
In case you want as a default parameter:
Table(string name="", vector<string> mods = vector<string>());
Like any other default parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With