Fork me on GitHub

6/29/2011

[C++] 使用預設參數值 實現 可變量參數

#include <iostream>
using namespace std;

void method(int a, int b) {
 int aa = a;
 int bb = b;
 cout << "aa:" << aa << " bb:" << bb << endl;
}

void defaultParaMethod(int a = 0, int b = 1) {
 int aa = a;
 int bb = b;
 cout << "aa:" << aa << " bb:" << bb << endl;
}

int main() {

 int A = 1;
 int B = 2;
 defaultParaMethod(A);
 defaultParaMethod(A, B);
 //method(a); // error

 return 0;
}

No comments:

Post a Comment