c++ - 具有默认值的参数化构造函数是什么意思?

标签 c++ constructor

我正在学习 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

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).

关于c++ - 具有默认值的参数化构造函数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418865/

相关文章:

c++ - : istringstream is( line ); 这一行发生了什么

C++: 'adding references' 到命名空间(和类库)?

c++ - 构造函数不会将使用 new 初始化的数组作为参数

java - java.lang.Object x = new Foo() 的 C++ 等价物是什么?

hibernate - JPA 2.0 : How to avoid full class name in `SELECT NEW full.class.Name` ?

c++ - 将迂腐的 GCC key 应用于 Qt 中的项目时出现很多警告

c++ - CUDA - 确定共享内存中的银行数量

c++ - Posix 线程 -(互斥量和条件变量)问题,

Java深度克隆问题

c++ - 使用重载运算符 () 在 C++ 中复制构造函数