构造函数C ++的常量引用

时间:2014-11-10 15:53:09

标签: c++ constructor reference constants

我在C ++中有这段代码

class ConstantTest
{
public:
    typedef set<string> StrSet;

    ConstantTest (const uint16_t & id = 0, const string & str = "Empty", 
                  StrSet & set = StrSet()) : 
                  // Works with: const StrSet & set = StrSet()
                  m_id (id), m_str (str), m_set (set) 
    { }
private:
    uint16_t m_id;
    string   m_str;
    StrSet   m_set;
};

我想知道为什么在const参数中没有StrSet关键字的情况下它无效。关键字工作得很好,但没有它我收到这条消息:

test_const.cpp:17:41: error: could not convert ‘std::set<std::basic_string<char> >()’ from ‘ConstantTest::StrSet {aka std::set<std::basic_string<char> >}’ to ‘ConstantTest::StrSet& {aka std::set<std::basic_string<char> >&}’
                   StrSet & set = StrSet()) :
                                         ^

0 个答案:

没有答案