Рассмотрим следующий код:
#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();
}
Могу ли я узнать, почему я не получил ошибку компилятора от
a* aa = (*iterator);
Я хочу, чтобы компилятор сказал мне, что мне нужно использовать const_iterator следующим образом:
const a* aa = (*iterator);
Или это мое неправильное ожидание от const_iterator?