-1
的C++繼承我有與N-一個抽象類保護成員:抽象參考成員的多態性
class Something {
protected:
UINT someVal;
std::vector<SomeType> g_MyVec;
// some virtual abstract methods ...
public:
UINT getSomeVal() { return someVal; }
std::vector<SomeType> GetVec() { return g_MyVec; }
}
class SubClass : public Something {
public:
SubClass() { // no members in this class, all inherited from super
someVal = 5; // this sticks
g_myVec = { .. correct initialization }; // this doesn't stick
}
}
該代碼的客戶端執行:
Something* s = &SubClass();
s->getSomeVal(); // OK, has 5 in it.
s->GetVec(); // Nada, it returns 0 size, nothing at all... WHY?!
啓示是非常讚賞。
你正在服用的臨時的地址???它是一個UB和不正確的代碼。 '子類'在';'之後與矢量一起被銷燬';' – Arunmu
'UINT'的類型是什麼?它是'unsigned int'嗎? uint32_t的? uint16_t? uint8_t?您應該查看'stdint.h'中的類型。 –
@ThomasMatthews看起來像[Windows API數據類型](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v = vs.85).aspx)(但只是猜測)和你的評論無疑是相關的。 –