此代碼不能用gcc 4.7.0編譯:刪除拷貝構造函數結果中刪除默認的構造函數
class Base
{
public:
Base(const Base&) = delete;
};
class Derived : Base
{
public:
Derived(int i) : m_i(i) {}
int m_i;
};
的錯誤是:
c.cpp: In constructor ‘Derived::Derived(int)’:
c.cpp:10:24: error: no matching function for call to ‘Base::Base()’
c.cpp:10:24: note: candidate is:
c.cpp:4:2: note: Base::Base(const Base&) <deleted>
c.cpp:4:2: note: candidate expects 1 argument, 0 provided
換句話說,編譯器不產生基類的默認構造函數,而是嘗試調用刪除的拷貝構造函數作爲唯一可用的重載。
這是正常的行爲嗎?
是的,因爲如果任何構造函數是*用戶聲明*,則默認構造函數被抑制。 – Xeo
爲什麼要刪除一個被認爲是構造函數聲明的構造函數?這是違反直覺的。 – kounoupis