考虑以下:
class A {
public:
const int c; // must not be modified!
A(int _c)
: c(_c)
{
// Nothing here
}
A(const A& copy)
: c(copy.c)
{
// Nothing here
}
};
int main(int argc, char *argv[])
{
A foo(1337);
vector<A> vec;
vec.push_back(foo); // <-- compile error!
return 0;
}
显然,复制构造函数是不够的。我错过了什么?
编辑:
Ofc。我无法改变this->c
方法operator=()
,所以我看不到operator=()
将如何使用(尽管 要求std::vector
)。