Archived

This topic is now archived and is closed to further replies.

pcxmac

working with templates, and initializing classes

Recommended Posts

my problem is template (class T) class DERIVED : BASE { DERIVED( T* classPtr, void (T::*member) (void* _var) ); ... }; class SOMECLASS { void func(void* _var); ... }; main() { DERIVED myDerived(&someClass, ); ... }; my question is how do i type in the second argument correctly

Share this post


Link to post
Share on other sites
First off (you probably did it to prevent forum formatting, but I''ll just be carefull), the line
template (class T)
should be
template < class T >


I think you need to explain what that second param even is

void (T::*member) (void* _var)

Looks like a function with one arguement which is a pointer to type void.

My guess is that you want the param to be a pointer to a function in the T which returns void, and takes a void * param... If so try using a typedef to define the function pointer... Really using function pointers you won''t even need to worry about the scope (T: or T at all.

Pat - Ex nihilo nihilus

Share this post


Link to post
Share on other sites
I've no idea why you need to do this, but the following code compiles with MinGW 3.2.3 (or whatever Dev-C++ 4.9.8.0 uses these days)

class BASE
{
public:
virtual ~BASE() {}
};

template<class T>
class DERIVED : public BASE
{
public:
DERIVED( T* classPtr, void (T::*member) (void* _var) ){};
DERIVED(){};
~DERIVED(){};
};

class SOMECLASS
{
public:
void func(void* _var);
};

int main()
{
SOMECLASS someClass;

typedef void (SOMECLASS::* pFUNC)(void *);

pFUNC pFunc = (pFUNC)&SOMECLASS::func;

DERIVED<SOMECLASS> myDerived(&someClass, someClass.*pFunc);

return 0;
}


EDIT : Interestingly, it does NOT compile on VC.NET (7.0) Pro

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on August 7, 2003 7:49:46 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
DERIVED myDerived(&someClass, &SOMECLASS::func );

Share this post


Link to post
Share on other sites
Hmm, would something like this work?


DERIVED<SOMECLASS> myDerived(&someClass, someClass.func);


This is how I'm tracing what's going on. The second parameter for your constructor expects a function pointer. So when we're doing the call we should be giving the address of a function. To get the address of the function we just omit the parentheses that follow the function name. Finally just add the name of the class instance this function is part of. So the final syntax should look something like that above. Haven't tested it yet so can't say if it'll work right.





--{You fight like a dairy farmer!}

[edited by - Greatwolf on August 7, 2003 9:51:21 PM]

Share this post


Link to post
Share on other sites
so i got it to work by doing this

Functor(classT) myFunctor(&someclass,&someclass::somefunc);

but then i get a linker error? doh i have deduced it to my constructor as if i take it away there are no linker errors.

anyone know about linker errors and template / functors.

also i am trying to use two base classes with the same overloaded virtual operator : virtual void operator() (void* var)

these two clases are inherited by a master class

and i want the base classes operators to both point to the same now defined operator in the master class. but i get a linker error too when trying to do this.

Share this post


Link to post
Share on other sites