Re: Functor Question - part II
On Nov 11, 6:38 pm, BSand0...@msn.com wrote:
[...]
There are several obvious errors in the code you posted. But
it's not the actual code, since it won't compile. (And you
really should strip out the namespace management macros junk
before posting. It may be necessary in your environment, but it
just causes confusion here.)
class TFunctor
{
public:
virtual void Call_m(STRUCT1*, STRUCT2*)=0;
};
template <class TClass> class TSpecificFunctor : public TFunctor
{
public:
// Constructor - takes pointer to an object and pointer to
// a member function and stores them in two private variables
TSpecificFunctor( TClass* _pt2Object, void(TClass::*tfpt)
(STRUCT1*, STRUCT2*) )
{ pt2Object = _pt2Object; fpt = tfpt; };
Where is pt2Object declared? What is its type (presumably
TClass*, but it could be a base class pointer, or even void*)?
// override function "call" in base class
virtual void Call_m(STRUCT1* bkgnd, STRUCT2* geo)
{ (*pt2Object.*fpt) (bkgnd, geo);
If pt2Object is a pointer, this statement isn't legal, and
shouldn't compile. If pt2Object is a TClass*, then what you
probably want is:
{ (pt2Object->*fpt)( bkgnd, geo ) ; }
Putting an additional parentheses around *pt2Object would also
work.
private:
void (TClass::*fpt) (STRUCT1*, STRUCT2*);
};
class BkgLand
{
public:
// Constructor/Destructor
BkgLand() {};
virtual ~BkgLand() {};
void Initialize();
void Update();
TFunctor::TFunctor *funcTable[];
Why TFunctor::TFunctor, instead of just TFunctor? I think it's
legal (because of type injection), but it is very confusing to
the reader, and I seem to recall encountering some compilers
which didn't accept it. (On seeing the name TFunctor after
TFunctor::, the compiler treated it as the "name" of a
constructor).
This won't compile anyway, of course, since you have to specify
the dimensions of a (non-static) member table.
};
void BkgLand::Initialize( )
cout << "REGISTER LAND FUNCTIONS" << endl;
// init_test function is a public member of the LandScats class
LandScats objLand;
TSpecificFunctor<LandScats> specFunc_0(&objLand,
&LandScats::init_test);
TFunctor::TFunctor *funcTable[] = { &specFunc_0 };
The above statement is a no-op. You define a local table of
pointers to TFunctor (again, same comment as above concerning
the name), with a dimension of 1, and initialize it with the
address of a local variable. You then immeately leave the
function, causing both the table and the local variable to
cease to exist. (Of course, the TSpecificFunctor instance
contains a pointer to still another local variable, and so
wouldn't be usable outside of the function anyway.
}
void BkgLand::Update()
{
//geom* and bkgndscat* are provided to this class via accessor
methods.
// Call appropriate registered function (can be land or water
based)
funcTable[0]->Call_m(bkgndscat, geom);
Here, of course, you use the funcTable variable of the object.
Since you've never initialized the contents of the table,
however, you've got totally undefined behavior.
}
BKGWATER.cpp Code (compiled in a separate library,
LIBWater.so, but LIBGround is linked in when this library is
built):
Has all of the problems of BKGLAND.cpp, so I won't bother
commenting.
Since your code won't compile, it's rather irrelevant to talk
about what it does on execution. If you want help with a
specific symptom, then you really should post the actual code
which causes that symptom. Otherwise, we really can't help you
much.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34