// 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";
}Question>使用VS2010时,情况A和情况B都可以顺利编译。
最初,我假设只有案例A有效,因为我记得默认参数应该在声明函数的地方引入,而不是在其定义的位置引入。有人能纠正我的错误吗?
谢谢
发布于 2013-08-21 02:43:02
如果您将默认参数放入方法定义中,那么只有那些看到该定义的人才能使用默认参数。唯一的问题是如果你尝试了这样的东西:
public:
Point(int i = 0, int j = 0);
(...)
Point::Point(int i = 0, int j = 0) { ... }那么你就会得到一个构建时错误。
//编辑:但是我很好奇Mark B.会在你的问题下面的评论中找到什么。
// EDIT2:而且clang编译器显然不喜欢大小写B。
https://stackoverflow.com/questions/18342472
复制相似问题