关于引用参数设置默认值的问题

最近遇到一个问题,就是要对一个vector的变量设置空的参数默认值,刚开始写NULL,发现不行,后来再网上查了一下,可以通过在外部设置一个变量,来为它赋值为空

#include <iostream>
#include <vector>
using namespace std;
vector<int> vc;
class A{
    public:
        A(vector<int> & vv=vc){
            v=vv;
            s="have value";
        }
        void show(){
            cout<<s<<endl;
            cout<<"v.size(): "<<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;
}

运行结果:

 

posted @ 2019-07-07 21:29  张杨  阅读(1348)  评论(0编辑  收藏  举报