Share via


編譯器警告 (層級 1 和層級 4,關閉) C4355

'this':用於基底成員初始設定式清單

this 指標僅在非靜態成員函式內有效。 它不能用於基底類別的初始設定式清單中。

基底類別建構函式和類別成員建構函式會在 this 建構函式之前被呼叫。 此模式與將未建構之物件的指標傳遞至另一個建構函式相同。 如果其他建構函式存取 this 上的任何成員或呼叫成員函式,則結果為未定義。 在完成所有建構之前,您不應該使用 this 指標。

此警告預設為關閉。 如需詳細資訊,請參閱 Compiler Warnings That Are Off by Default

下列範例會產生 C4355:

// C4355.cpp
// compile with: /w14355 /c
#include <tchar.h>

class CDerived;
class CBase {
public:
   CBase(CDerived *derived): m_pDerived(derived) {};
   ~CBase();
   virtual void function() = 0;

   CDerived * m_pDerived;
};

class CDerived : public CBase {
public:
   CDerived() : CBase(this) {};   // C4355 "this" used in derived c'tor
   virtual void function() {};
};

CBase::~CBase() {
   m_pDerived -> function();
}

int main() {
   CDerived myDerived;
}