How to define the access resolution to call a function pointer - class data member
I was trying to play around the messages posted; while doing so, i
came across something weird (atleast for me) to call a class level
function pointer like this
((*obj).*(((*obj).f)))()
If i do not specify the instance name 2nd time, MSVC8.0 complains that
'f' is undeclared identifier. Any idea about this weird access
resolution is required:
#include <algorithm>
#include <vector>
#include <functional>
class A{
public:
typedef void (A::*FN)();
FN f;
A(): f(&A::show){}
virtual void show()
{
std::cout<<"TEST A"<<std::endl;
}
virtual ~A(){}
};
class B : public A
{
public:
typedef void (B::*FN)();
FN f;
B(): f(&B::show){}
void show()
{
std::cout<<"TEST B"<<std::endl;
}
virtual ~B(){}
};
template<class T1, class T2>
struct IsOfT2Type : public std::unary_function<T1*, bool>
{
IsOfT2Type() : result(){}
std::vector<T1*> result;
bool operator()(T1* obj)
{
if(dynamic_cast<T2*>(obj) != 0)
{
result.push_back(obj);
return true;
}
return false;
}
};
struct CallShow : public std::unary_function<A*, void>
{
void operator()(A* obj)
{
(obj->*f)();
((*obj).*((f)))();
}
};
int main ()
{
std::vector<A*> col;
col.push_back (new A());
col.push_back (new B());
col.push_back (new A());
col.push_back (new A());
col.push_back (new B());
IsOfT2Type<A, B> objB = std::for_each(col.begin(), col.end(),
IsOfT2Type<A, B>());
for_each(objB.result.begin(), objB.result.end(), CallShow());
return 0;
}
leave the memory leaks for now!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]