最近遇到一个问题,就是要对一个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;
}

运行结果:

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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-25
猜你喜欢
  • 2022-12-23
  • 2021-10-18
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
相关资源
相似解决方案