开发者

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 wa开发者_运维知识库s C++.


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.


To add to the other answer: If you're using c++11, you can use the universal initialization syntax to shorten the default parameter declaration for a vector to the following:

Table(string name="", vector<string> mods={});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜