2011-01-22 44 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),并且如果有任何错误产生,很少会遇到任何问题。

干杯&心连心,

相关问题