這個問題是相當fundamental.I給我cygwin的一個簡單而straighfoward測試:單和經典構造
class Example {
public:
Example(){ cout<<"dude..."<<endl; }
~Example(){ cout<<"see ya"<<endl; }
public:
static Example *GetInstance(){
if(m_instance==NULL){
m_instance = new Example();
cout<<"watch out bro"<<endl;
}
return m_instance;
}
public:
void exp(){cout<<"greetings"<<endl;}
private:
static Example *m_instance;
};
int main(){
Example a;
return 0;
}
顯然,輸出爲:
dude...
greetings
see ya
技術上單和典型的構造是在C++編程中有很多不同的故事,因爲singleton將構造函數設置爲私有,而典型的方式則相反。在我最近的實習體驗中,我注意到大多數編程人員以這種方式實現API。
我只是想知道這是否是這種情況,或者當兩種類構建方法並行存在時都是不必要的。
UPDATE
是構造&單在一個程序幾乎廢話的Cuz在此範圍內的單身現有的東西會成爲像unhazardous垃圾無用的代碼?
摘要 這是一個非常扯淡的問題... 更糟糕的是,感謝所有的你brilliants 構造函數和Singleton設計模式是「互斥」,並在vulnerbility而言,它是與全局變量相同的故事會影響我們的調試時間...
A「單身」的對象,可以有兄弟姐妹(如本例中)僅僅是一個全局變量,所有的注意缺點。 – MSalters 2012-03-05 09:05:47