我读通过编程:原理和编程使用C++,我发现了这片的示例代码,真正让我感兴趣的函数初始化过程中。对于一直在使用C++的C++程序员来说,如何使用这个函数初始化语法有帮助?它对你的工作流程是否有帮助,对程序员来说是否更易读,还是只是C++中另一种秘密方式?它有助于节目表演吗?
我想知道有关它的更多一些,我无法找到使用我那可怜的谷歌技能它什么。
class Year {
static const int min = 1800;
static const int max = 2200;
public:
class Invalid {}; //this thing throws an error when called
Year(int x) : y(x) { if (x < min || max <= x) throw Invalid(); } //what is : ??
int year() { return y; }
private:
int y;
};
你问结肠做什么,或者什么初始化好? – Beta
http://en.cppreference.com/w/cpp/language/initializer_list – user657267
见http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-构造函数 – chris