关于引用参数设置默认值的问题
最近遇到一个问题,就是要对一个vector的变量设置空的参数默认值,刚开始写NULL,发现不行,后来再网上查了一下,可以通过在外部设置一个变量,来为它赋值为空
#include#include using namespace std; vector<int> vc; class A{ public: A(vector<int> & vv=vc){ v=vv; s="have value"; } void show(){ cout< endl; cout<<"v.size(): "<endl; } private: vector<int> v; string s; }; int main(){ vector<int> s={1,2,3,4}; A a(s); A b; a.show(); b.show(); return 0; }
运行结果: