C++ left arrow operator
Posted on July 29, 2016
Sometimes you have a pointer to a class, and you want to invoke a method. You can use the ->
operator for that.
So what do you do when you have a pointer to a method, and want to invoke it on a class? Use the <-
operator!
#include <iostream>
template<class T>
struct larrow {
(T* a_) : a(a_) { }
larrow* a;
T};
template <class T, class R>
operator<(R (T::* f)(), larrow<T> it) {
R return (it.a->*f)();
}
template<class T>
<T> operator-(T& a) {
larrowreturn larrow<T>(&a);
}
struct C {
void f() { std::cout << "foo\n"; }
};
int main() {
;
C x(&C::f)<-x;
}