函數(shù)
6.1函數(shù)基礎(chǔ)
函數(shù)的形參列表
函數(shù)的返回類型
-
含有可變形參的函數(shù)
主要用于輸出錯誤日志猾漫,如果實參類型相同可以用initializer_list
的標準庫纽甘,實參類型不同傻寂,使用可變參數(shù)模板帽揪。書中16.4節(jié)介紹拢肆。
C++ 還有特殊的形參類型(即省略符)烁兰,注意這種功能一般只用于與C函數(shù)交互的接口程序耐亏。#include<iostream> #include<system_error> using namespace std; void error_msg(initializer_list<string> il) { for(auto beg = il.begin(); beg != il.end(); ++beg) cout<< *beg<< " "; cout << endl; } void error_msg(std::error_code e, initializer_list<string> il) { cout<< e.message() <<": "; for(const auto &elem : il) cout << elem << " "; cout << endl; } int main() { string m_szA = "hello"; string m_szB = "world"; std::error_code iErrorCode ; if(m_szA!= m_szB) { error_msg({"main",m_szA,m_szB}); } if(m_szA!= m_szB) { error_msg(iErrorCode,{"main",m_szA,m_szB}); } return 0; }
省略符形參
6.3 返回類型和return語句
- 無返回值函數(shù)