-3
一個問題之間。根據此代碼C++ /本和靜態類名
Cl& Cl::getInstance()
{
static Cl instance;
return instance;
}
我怎麼通過這個代碼實現和區別是,如果我將返回this
差異。
*這個方法是靜態
一個問題之間。根據此代碼C++ /本和靜態類名
Cl& Cl::getInstance()
{
static Cl instance;
return instance;
}
我怎麼通過這個代碼實現和區別是,如果我將返回this
差異。
*這個方法是靜態
如果該方法是靜態的,this
不隱式定義,所以這個問題不適用。
另一方面,如果方法是非靜態成員,則會有很大的差異。
Cl& Cl::getInstance()
{
static Cl instance;
return instance;
}
在這裏你總是返回,即使從同一類的幾個實例調用同一個實例:一個單(具誤導爲返回的實例無關與呼叫實例)
Cl& Cl::getInstance()
{
return *this;
}
上面,你返回當前實例(不是極大的興趣...)
編輯:也許你的問題是有關singleton design pattern在沒有對象可以得到有效的不使用getInstance()
因爲構造函數是私有的,而在這種情況下,有趣的是它返回相同的實例爲每個呼叫者0對象:
Cl& Cl::getInstance() // static method
{
static Cl instance; // constructor is private, only can be called from here
return instance;
}
沒有'this'在靜態方法(我假設這種方法靜態的)。 – tkausl
沒有人說這是一種靜態方法。它只是返回一個靜態變量。 –
赦免,我忘了補充,這種方法是靜態的 – malocho