I have a problem with iterator and their supposed pointer equivalence...
I thought that an iterator was an intelligent pointer, pointing to the object it is supposed to describe.
But it seems that this pointer equivalence is not so obvious.
Look at the following code : I would like to be able to call the function f, passing by an iterator and it does not work directly...
Code: Select all
class T
{
public:
int x;
int get() { return(x); };
};
int read(T* t)
{
return(t->x);
}
void Test()
{
std::vector<T> vec = { {0},{2},{4} };
for (auto i = vec.begin(), e = vec.end(); i != e; ++i)
{
std::cout << (*i).x << std::endl; // ok, iterator seems to be a T*
std::cout << i->x << std::endl; // ok, iterator seems to be a T*
std::cout << i->get() << std::endl; // ok, iterator seems to be a T*
std::cout << read(i) << std::endl; // cannot cast iterator into T* !!!
std::cout << read(&(*i)) << std::endl; // this trick works !!!
}
}
Thanks for your help.