[英]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.