2011-05-16 67 views
7

假設的靜態初始化我有一個類T其中C++類無國籍

  1. T沒有虛函數。
  2. T實例沒有狀態。
  3. T有自己的靜態成員實例。
  4. T本身沒有其他狀態。

C++靜態初始化失敗可以毀了我的程序嗎?我不這麼認爲,因爲即使其中一個靜態實例在使用之前未被初始化,也不應該擔心,因爲T對象是無狀態的。

我很感興趣,這樣做,枚舉類類,像這樣:


// Switch.h 

class Switch { 
public: 
    static Switch const ON; 
    static Switch const OFF; 
    bool operator== (Switch const &s) const; 
    bool operator!= (Switch const &s) const; 
private: 
    Switch() {} 
    Switch (Switch const &); // no implementation 
    Switch & operator= (Switch const &); // no implementation 
}; 

// Switch.cpp 

Switch const Switch::ON; 
Switch const Switch::OFF; 

bool Switch::operator== (Switch const &s) const { 
    return this == &s; 
} 

bool Switch::operator!= (Switch const &s) const { 
    return this != &s; 
} 
+3

優秀的問題和一個有趣的想法! – 2011-05-16 22:06:58

+0

這是什麼讓你通過一個簡單的枚舉(沒有額外的狀態)? – Nim 2011-05-16 22:08:46

+0

請發表一個你如何真正打算使用Switch類的例子。 – 2011-05-16 22:10:52

回答

2

要回答你的問題的第一部分,如果T有一個構造函數有副作用,那麼你實際上可以通過靜態初始化慘敗。

0

你真的打算使用指針值比較「狀態」?我同意@德魯,這是一個有趣的想法。不過,我不確定標準是否能夠保證它能夠正常工作,但是,如果我們假設這是一個只有標題的實現。

請考慮當多個編譯對象包含Switch::ONSwitch::OFF的相同定義時會發生什麼情況。由於這些是變量,而不是函數,鏈接器必須在它們之間任意決定。

當您運行測試時,流行的編譯器會說什麼:gcc 3,gcc 4,microsoft C++ 2005,2008和2010,以及Edison Design Groups的編譯器之一如http://www.comeaucomputing.com/

該測試將包括:

// Switch.h 
class Switch { 
public: 
    static Switch const ON; 
    static Switch const OFF; 
    bool operator== (Switch const &s) const; 
    bool operator!= (Switch const &s) const; 
private: 
    Switch() {} 
    Switch (Switch const &); // no implementation 
    Switch & operator= (Switch const &); // no implementation 
}; 

Switch const Switch::ON; 
Switch const Switch::OFF; 

bool Switch::operator== (Switch const &s) const { 
    return this == &s; 
} 

bool Switch::operator!= (Switch const &s) const { 
    return this != &s; 
} 

// main.cpp 
#include "Switch.h" 

extern int another_test(); 

int main(int argc, char*argv[]) 
{ 
    another_test(); 
    const Switch& current_state = Switch::ON; 
    const Switch& another_state = Switch::OFF; 
    if (current_state == another_state) { 
    return 1; 
    } else if (current_state != another_state) { 
    return 2; 
    } 
    return another_test(); 
} 

// another_test.cpp 
#include "Switch.h" 

int another_test() 
{ 
    const Switch& current_state = Switch::ON; 
    const Switch& another_state = Switch::OFF; 
    if (current_state == another_state) { 
    return 4; 
    } else if (current_state != another_state) { 
    return 5; 
    } 
    return 6; 
} 
2

我感興趣的是什麼,你,比如說,從一個枚舉看到優勢包裹在命名空間或類中:

namespace Switch { 
    enum Switch { 
     ON, 
     OFF 
    }; 
} 

在大多數情況下使用會更簡單(在您的實現中,您需要用戶使用引用或指針,因爲對象是不可複製的),它需要更少的代碼(不需要禁用構造函數,並且創建運營商)...

由於事實上,在即將到來的標準,你幾乎拿到免費的,甚至沒有使用的命名空間:

enum Switch { 
    ON, 
    OFF 
}; 
// bad, it allows this (as in the current standard): 
Switch s = ON; 
// good, it does also allow explicit qualification: 
Switch s = Switch::ON; 
+1

我從來沒有考慮過這個。雖然是惡魔的倡導者,但我提出的類別變體不允許隱式轉換爲整數。你也不能重新打開一個類,但是可以通過包裝在一個類中而不是我想要的名稱空間來解決。 – 2011-05-17 00:58:03

相關問題