Consider the following code :
#include <vector>
#include <iostream>
class a {
public:
int i;
void fun() { i = 999; }
void fun() const { std::cout << "const fun" << std::endl; }
};
const a* ha() {
return new a();
}
int main()
{
std::vector<a *> v;
v.push_back(new a());
// cannot convert from 'const a *' to 'a *'
// a* test = ha();
std::vector<a *>::const_iterator iterator = v.begin();
for (; iterator != v.end(); ++iterator) {
// No enforcement from compiler? I do not want user to modify the content through
// const_iterator.
a* aa = (*iterator);
aa->fun();
}
std::cout << (*(v.begin()))->i << std::endl;
getchar();
}
May I know why I didn't get compiler error from
a* aa = (*iterator);
I wish compiler will tell me I need to use the const_iterator in the following way :
const a* aa = (*iterator);
Or, this is my wrong expectation on const_iterator?