views:

69

answers:

2

Hi,

I think the title is clear on explaining my problem.... consider the following snippet:

class Critter {
    int m_Age;
};

int main()
{
    vector<Critter* const> critters;
    for(int i = 0; i < 10; ++i)
        critters.push_back(new Critter());

    critters[2] = new Critter();

    return 0;
}

Shouldn't the line critters[2] = new Critter(); be illegal?

Thank You

+5  A: 

Actually this line should be illegal (even given #include <vector> and using std::vector;):

vector<Critter* const> critters;

Because it is a requirement for a type to be used in a container to be assignable and anything that is const clearly isn't.

Charles Bailey
A: 

Sorry, I didn't post the entire code because what's missing are only the includes and using... anyway here is the entire code:

#include <iostream>
#include <vector>

using namespace std;

class Critter {
    int m_Age;
};

int main()
{
    vector<Critter* const> critters;
    for(int i = 0; i < 10; ++i)
        critters.push_back(new Critter());

    critters[2] = new Critter();

    return 0;
}

And this code as it is, compiles fine, even without a warning in VS 2010... Thanks once more...

Hugo