的代碼片段下面是從我的Keyboard.h
和Keyboard.cpp
。我的問題是,我將如何實現分層的父母和孩子的關係?因此,如果孩子沒有實施任何虛擬成員,父母可以自由地這樣做。作爲一個例子,孩子實現了KeyboardDepressA,但沒有實現KeyboardDepressB,讓父母實現了KeyboardDepressB的捕獲。同時父母和孩子都是不同類型的課程,什麼不可以的......虛擬父子關係
我不想讓孩子知道父母對Device::Keyboard
支持;因此,我想使用get實例調用父級,如KeyboardDepressB
中所示。爲了使這項工作,我需要到父分配給static
實例本身,而是static
劑量沒有爲關鍵字this
支持。所以我該怎麼做?
我不停的代碼到最低限度。如果需要更多,請隨時詢問。 ^^
class Keyboard
{
private:
Keyboard();
~Keyboard();
public:
static Device::Keyboard& Instance();
static void SetParent(Device::Keyboard *cpParent);
virtual void KeyboardDepressA ();
virtual void KeyboardReleaseA ();
virtual void KeyboardDepressB ();
virtual void KeyboardReleaseB ();
… … ...
};
Device::Keyboard&
Keyboard::Instance()
{
static Device::Keyboard nrKeyboard;
return nrKeyboard;
} // member
void
Keyboard::SetParent(Device::Keyboard *cpParent)
{
Device::Keyboard& nrParent(Keyboard::Instance());
*this = cpParent;
} // member
void
Keyboard::KeyboardDepressA()
{
Device::Keyboard& nrParent(Keyboard::Instance());
nrParent.KeyboardDepressA();
} // member
void
Keyboard::KeyboardReleaseA()
{
Device::Keyboard& nrParent(Keyboard::Instance());
nrParent.KeyboardReleaseA();
} // member
void
Keyboard::KeyboardDepressB()
{
Device::Keyboard& nrParent(Keyboard::Instance());
nrParent.KeyboardDepressB();
} // member
void
Keyboard::KeyboardReleaseB()
{
Device::Keyboard& nrParent(Keyboard::Instance());
nrParent.KeyboardReleaseB();
} // member
單類通常使它們的構造函數/析構函數私人 – cppguy
TY。我知道...這仍然是在進行中..如果我選擇使用實例設計,我將不得不使它成爲單身。 ^^ –