簡體   English   中英

帶默認值的參數化構造函數是什么意思?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM