Using a class with const data members in a vector
Posted
by Max
on Stack Overflow
See other posts from Stack Overflow
or by Max
Published on 2010-05-26T22:20:15Z
Indexed on
2010/05/26
22:21 UTC
Read the original article
Hit count: 209
Given a class like this:
class Foo
{
const int a;
};
Is it possible to put that class in a vector? When I try, my compiler tells me it can't use the default assignment operator. I try to write my own, but googling around tells me that it's impossible to write an assignment operator for a class with const data members. One post I found said that "if you made [the data member] const that means you don't want assignment to happen in the first place." This makes sense. I've written a class with const data members, and I never intended on using assignment on it, but apparently I need assignment to put it in a vector. Is there a way around this that still preserves const-correctness?
© Stack Overflow or respective owner