[英]What does it mean by parameterized constructor with default value?
我正在学习c ++。 我制作了该程序,但是在编译时,该程序显示出模棱两可的错误。 我不明白,如果我要创建一个没有参数的对象,那么它应该只调用默认构造函数,并且程序应该运行而没有任何错误。 这是代码:
#include<iostream>
using namespace std;
class room
{
int length,width;
public:
room()
{
cout<<"Default constructor";
}
room(int l=0)
{
cout<<"Constructor"; //the compiler is taking this also as default constructor
}
room(int l=0,int w=0)
{
cout<<"Constructor"; //the compiler is taking this also as default constructor on making other two as comment
}
};
int main()
{
room r1;
return 0;
}
我已经在诸如Codeblocks,Dev c ++和GCC之类的编译器上尝试过此代码。
room r1
模棱两可,因为所有参数均默认为默认值的构造函数已经可以作为room()
用作默认构造函数
第12.1节
X类的默认构造函数是X类的构造函数, 无需参数即可调用它 。 如果没有用户声明的类X构造函数,则将不带参数的构造函数隐式声明为默认值(8.4)。
您有3个无需提供任何参数即可调用的构造函数。 因此,这3个构造函数会使编译器感到困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.