const pod and std::vector

Posted by Baz on Stack Overflow See other posts from Stack Overflow or by Baz
Published on 2012-10-22T16:55:07Z Indexed on 2012/10/22 17:00 UTC
Read the original article Hit count: 133

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about vector