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,这不是我想要的。