爲了更明確一些,當我使用()創建對象時嘗試訪問實例變量時出現編譯時錯誤,但是當我沒有時,代碼編譯並按預期方式運行。另外,這個問題只適用於默認的構造函數。 我想明白爲什麼。Object b();有什麼區別?和對象b;?
using namespace std;
#include <iostream>
class Student {
public:
int gpa;
Student() {
gpa = 4;
}
Student(int x) {
gpa = x;
}
};
int main() {
Student zero;
Student sally(2);
Student jack();
cout << zero.gpa << endl; //prints 4
cout << sally.gpa << endl; // prints 2
cout << jack.gpa << endl; //error: request for member 'gpa' in 'jack', which is of non-class type 'Student()'
}
你爲什麼不使用新的? –
這不是嘗試解決問題的問題。我想了解爲什麼我首先遇到了錯誤。 –
@huseyintugrulbuyukisik他爲什麼要呢?只要適用,自動生命週期就會跳過動態生命週期。 – delnan