[英]In C++, when no constructor is declared for a class, what will happen if I construct an object with arguments?
我有 struct student 並且我沒有聲明構造函數。 如果我執行以下操作,會發生什么?
struct student{
int assns, mt, finalExam;
float grade(){…}
}
student billy (60, 70, 80);
這個答案是根據問題標題而不是正文編寫的,因為它們似乎存在嚴重沖突,希望 OP 對此進行編輯。
您將在編譯期間遇到錯誤。
代碼:
#include <iostream>
class test
{
int tt;
};
int main ()
{
test t1 (34);
}
編譯器錯誤:
In function 'int main()':
10:17: error: no matching function for call to 'test::test(int)' 10:17: note: candidates are:
2:7: note: test::test()
2:7: note: candidate expects 0 arguments, 1 provided
2:7: note: constexpr test::test(const test&)
2:7: note: no known conversion for argument 1 from 'int' to 'const test&'
2:7: note: constexpr test::test(test&&)
2:7: note: no known conversion for argument 1 from 'int' to 'test&&'
發生這種情況是因為沒有定義帶參數的構造函數。 沒有 ctor 就沒有類的意義,因為你永遠無法初始化它的數據成員,如果建築公司本身不存在,你怎么能期望構造一些東西。
編譯器會拋出錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.