[英]Need help understanding the output
我有以下代碼
#include <iostream>
using namespace std;
class Base {
private:
Base() {
cout<<"Base ctor has been called "<<endl;
};
public:
int a;
};
class Derived: public Base {
};
int main() {
Base* b1;
Derived d1();
cout<<d1;
return 0;
}
我不確定在上面的示例中如何創建派生的 class object d1。 代碼編譯並運行沒有任何錯誤。 創建Derived d1();
? 為什么 output 的cout<<d1;
是1嗎? Derived 的實例是否具有基數 class 的int a
? 感謝您的幫助!
在你的main
function 中, Derived d1();
實際上是一個名為d1
的 function 的聲明,它不接受 arguments 並返回一個Derived
。 至於為什么打印1
, std::ostream
沒有Derived (*)()
( d1
被隱式轉換為)的重載,因此它將它轉換為唯一合適的類型,即bool
。 嘗試更換cout<<d1;
與cout << boolalpha << d1;
你會看到程序打印true
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.