Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default parameter for a vector <string> for use in a default constructor within a class?

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++.

like image 440
Omar Avatar asked Dec 06 '09 03:12

Omar


People also ask

Can a class have a constructor with default parameters?

Yes, a constructor can contain default argument with default values for an object.

How do I set the default value of vector as in CPP?

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.

What is the default type of argument used in constructor?

The default constructor with argument has a default parameter x, which has been assigned a value of 0.


1 Answers

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.

like image 198
aJ. Avatar answered Oct 26 '22 14:10

aJ.