2011-01-22 99 views
0

我需要有人向我解釋這段代碼。我特別不明白這一行:我需要有人向我解釋這幾行代碼

operator std::map<T, U>() 

非常感謝。

template <typename T, typename U> 
class create_map 
{ 
    std::map<T, U> m_map; 
public: 
    create_map(const T& key, const U& val) 
    { 
     m_map[key] = val; 
    } 
    create_map<T, U>& operator()(const T& key, const U& val) 
    { 
     m_map[key] = val; 
     return *this; 
    } 
    operator std::map<T, U>()  
    { 
     return m_map;  
    } 
}; 
+0

請重新格式化您的代碼正確(在編輯器中選中它,然後點擊「編碼」工具按鈕) – datenwolf 2011-01-22 16:18:06

回答

6
operator std::map<T, U>()  
{ 
     return m_map;  
} 

這是用戶定義轉換功能。

這意味着,你可以這樣寫:

//obj is an object of type create_map 
create_map<int,std::string> obj(1,"Nawaz"); 

//obj implicitly converts into std::map type! 
std::map<int,std::string> map_inst=obj; 

請參見本主題以瞭解更多關於用戶定義的轉換函數

User Defined Conversions in C++

你可以看到這個以及:Implicit conversion sequences (C++ only)


create_map<T, U>& operator()(const T& key, const U& val) 
{ 
     m_map[key] = val; 
     return *this; 
} 

這實際上重載operator(),其內部插入或更新(鍵,VAL)配對到m_map;只是看它的功能定義。

因此,使用該功能你可以寫這樣的代碼,

obj(2,"Sarfaraz"); //this inserts the pair into the underlying m_map; 

我也建議你去探索std::map多一點,尤其是在std::map重載operator[]

+0

對不起,我真的不understandthat。我真的需要一行一行的解釋,真的很重要,謝謝。 – 2011-01-22 16:24:34

+0

所以它只是在必要時將create_map對象轉換爲地圖對象?你能解釋一下其他運算符的重載嗎?以及它如何使用?即時通訊對不起,這種小白菜。非常感謝你。 – 2011-01-22 16:31:30

+0

@ jose:的確如此。 – Nawaz 2011-01-22 16:32:16

-1

operator std::map<T, U>() 

定義,當你create_map對象用於像的std ::地圖某處的代碼將被調用的函數。

一個簡單的例子是:

class A 
{ 
public: 
    operator int() 
    { 
    return 3; 
    } 
}; 

int main() 
{ 
    A a; 
    cout << a << endl; 
} 

現在的電腦發現,它不知道如何打印變量,但它知道如何將它轉換成int,然後打印出來。所以打印出「3」。

0

對此沒有多少了解。 operator std::map<T, U>()將重寫類的轉換運算符(不帶參數)以提供std::map<T, U>類型的對象實例。 std::map是用於關聯鍵值存儲的STL標準類。在你的情況下,它從T類型的鍵映射到U類型的值。 TU已經未定義到目前爲止(你寫template class,但如果是模板參數?)

轉換操作符允許使用的類的實例來代替運營商提供轉換的,像這樣的類型。

class foo { 
    operator char const *() { 
     return "foo instance as char const *"; 
    } 
}; 

// ... 

void bar(foo &f) 
{ 
    // here the class instance is used as if it were a char const * 
    cout << f << endl; 
} 
1

代碼:

template <typename T, typename U> 
class create_map 
{ 
    std::map<T, U> m_map; 
public: 
    create_map(const T& key, const U& val) 
    { 
     m_map[key] = val; 
    } 
    create_map<T, U>& operator()(const T& key, const U& val) 
    { 
     m_map[key] = val; 
     return *this; 
    } 
    operator std::map<T, U>()  
    { 
     return m_map;  
    } 
}; 

這段代碼的目的是爲了能夠指定特定的鍵/值對的映射,通過鏈接調用operator(),像

create_map<int, std::string>(1, "blah")(2, "hah")(3, "doh") 

由於該類沒有默認構造函數,因此無法使用它來創建空映射。這可能是設計。或者它可能是一個設計錯誤。

operator std::map<T, U>()  
{ 
    return m_map;  
} 

限定了轉換到std::map<T, U>,這是它的所有的最終結果。

無論指定create_map還是std::map,都可以隱式調用它,例如在函數調用中使用create_map表達式作爲參數。

但是,它可能是非常低效的,因爲它複製地圖。編譯器可能會優化複製。但它不必要地依賴於實施質量(儘管有時候這是最好的)。

所以應該改爲

operator std::map<T, U> const&() const 
{ 
    return m_map;  
} 

const底有允許create_map聲明爲const和使用。

將此轉換爲參考時,存在與使用參考參數相同的問題,即理論上存在混疊的可能性,其中保留對const的引用的代碼不準備處理引用對象的更改。但實際上它不是一個問題。例如,作爲正式的論點,我們當然會寫std::string const& s(而不僅僅是std::string s),並且如果有任何錯誤產生,很少會遇到任何問題。

乾杯&心連心,

相關問題