關於引用參數設置默認值的問題


最近遇到一個問題,就是要對一個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;
}

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM