这段代码是做什么的?更准确地说,测试tob();做什么?
class test {
private:
int a;
int b;
public:
test (int);
test();
};
test::test() {
cout<<"default";
}
test::test (int x=0) {
cout<<"default x=0";
}
int main() {
test tob();
}我不知道测试tob();做什么,但是它没有给出任何编译错误。
发布于 2013-10-11 10:48:35
test tob();这个声明是一个返回类型为test的函数。它不创建对象,,也称为最烦人的解析。
要创建test对象,请执行以下操作:
test tob;此外,使用默认参数定义函数(包括构造器)的方式是不正确的。
test::test (int x=0) { // incorrect. You should put it in function when it's first declared
cout<<"default x=0";
}下面的代码应该有效:
class test {
int a;
int b;
public:
explicit test (int = 0); // default value goes here
};
test::test (int x) {
cout<<"default x=0";
}
int main() {
test tob; // define tob object
}https://stackoverflow.com/questions/19316298
复制相似问题