2018年6月21日 星期四

不定長度的引數-Openhome.com

不定長度的引數
(1)引入#include

#include  
#include  
using namespace std; 
void foo(int, ...); 
int main() { 
    double x = 1.1, y = 2.1, z = 3.9; 
    double a = 0.1, b = 0.2, c = 0.3; 
    cout << "三個參數:" << endl; 
    foo(3, x, y, z); 
    cout << "六個參數:" << endl; 
    foo(6, x, y, z, a, b, c); 
    return 0; 
} 
void foo(int i, ...) { 
    double tmp; 
    va_list num_list; //(1)巨集參數
    va_start(num_list, i); //(2)開啟巨集
    for(int j = 0; j < i; j++) 
        cout << va_arg(num_list, double) << endl; //(3)取出巨集
    va_end(num_list); //關閉巨集
}

沒有留言:

張貼留言

Ethereum- Learn Solidity step by step

Common Function Types: public: Anyone can call this function,but it isn't really used for any type of security per se. priv...