[英]Location of C++ function default parameters
// Case A
class Point {
private:
int x;
int y;
public:
Point(int i = 0, int j = 0); // Constructor
};
Point::Point(int i, int j) {
x = i;
y = j;
cout << "Constructor called";
}
// Case B:
class Point {
private:
int x;
int y;
public:
Point(int i, int j); // Constructor
};
Point::Point(int i = 0, int j = 0) {
x = i;
y = j;
cout << "Constructor called";
}
問題>案例A和案例B編譯都沒有VS2010的問題。
原文我假設只有案例A有效,因為我記得應該在聲明函數的地方而不是其定義的位置引入默認參數。 有人可以糾正我嗎?
謝謝
如果將默認參數放入方法定義中,則只有看到定義的人才能使用默認參數。 唯一的問題是如果你嘗試過這樣的事情:
public:
Point(int i = 0, int j = 0);
(...)
Point::Point(int i = 0, int j = 0) { ... }
然后你會得到一個構建時錯誤。
//編輯:但我很好奇Mark B.會在你的問題評論中提到的內容。
// EDIT2:顯然clang編譯器也不喜歡Case B.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.