const pod and std::vector
- by Baz
To get this code to compile:
std::vector<Foo> factory()
{
std::vector<Foo> data;
return data;
}
I have to define my POD like this:
struct Foo
{
const int i;
const int j;
Foo(const int _i, const int _j): i(_i), j(_j) {}
Foo(Foo& foo): i(foo.i), j(foo.j){}
Foo operator=(Foo& foo)
{
Foo f(foo.i, foo.j);
return f;
}
};
Is this the correct approach for defining a pod where I'm not interested in changing the pod members after creation? Why am I forced to define a copy constructor and overload the assignment operator? Is this compatible for different platform implementations of std::vector? Is it wrong in your opinion to have const PODS like this? Should I just leave them as non-const?