Casting pointers to objects

Is there a way to cast a pointer so that instead of using
(*iterator)->I can just use
iterator->?
Last edited on
Cubbi's solution is probably the most professional. If you, however, would like to skip using boost, see if this is acceptable for you. I did not test and requires C++11. May or may not work because I really haven't caught up on C++11. I may have conceptual errors.

1
2
3
4
5
6
7
8
9
10
11
template<class T, class U>
T inline __dereference(U &it) { return (*it); }

#define DR(x) auto &drit = __dereference(x)

//And then you use that like this:
for (std::vector<MyClass*>::iterator it = myVec.begin(); it != myVec.end(); ++it)
{
    DR(it);
    drit->MemberMethodOfMyClass();
}


Caveats:

1. The new variable name is hardcoded. You can change that, though.
2. Might be a big fat lot in comparison to boost?
3. Untested. Might not even be valid C++11. :-(
Topic archived. No new replies allowed.