2
我試圖在類中存儲boost::program_options::options_description
,但我不能爲我的課寫assignment operator,因爲options_description有一個const成員。或者至少我是這樣理解問題的。複製分配助推options_descriptions
這裏是我的類的實例,將無法編譯:
struct command
{
command()
{
}
command(const std::string& name,
const po::options_description& desc)
: name(name), desc(desc)
{
}
command& operator=(const command& other)
{
name = other.name;
desc = other.desc; // problem here
return *this;
}
~command()
{
}
std::string name;
po::options_description desc;
};
/usr/include/boost/program_options/options_description.hpp:173:38:
error: non-static const member
‘const unsigned int boost::program_options::options_description::m_line_length’,
can’t use default assignment operator
/usr/include/boost/program_options/options_description.hpp:173:38:
error: non-static const member
‘const unsigned int boost::program_options::options_description::m_min_description_length’,
can’t use default assignment operator
本來這是一個自我回答問題。然後我意識到:
command& operator=(const command& other)
{
name = other.name;
desc.add(other.desc);
return *this;
}
會將other.desc附加到desc,這不是我想要的。