Skip to main content
GameDev.net gamedev.net
🔒 Locked

working with templates, and initializing classes

Started by pcxmac Aug 7, 2003 at 6:12 PM 7 replies 350+ views
Original Post
pcxmac
pcxmac
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
sig
patindahat
patindahat
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
zealouselixir
zealouselixir
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]
[twitter]warrenm[/twitter]
pcxmac
pcxmac
its for a functor class
sig
zealouselixir
zealouselixir
AP''s solution doesn''t work unless the member is static (maybe not even then; I didn''t bother testing, but it certainly doesn''t work as-is).
[twitter]warrenm[/twitter]
Greatwolf
Greatwolf
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]
--{You fight like a dairy farmer!}
pcxmac
pcxmac
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.
sig
zealouselixir
zealouselixir
Once again, if you insist on doing it that way, the method must be static.
[twitter]warrenm[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.