Click to See Complete Forum and Search --> : Calling static member function from non-class function


djwhy
July 18th, 2008, 02:47 AM
I'm having trouble calling a static class member function (that returns a pointer to a singleton) from a non-class function. When I try to compile (with mingw gcc) I get an error. The problem code is below:

void executer(void* params)
{
FunctionParameters* args = static_cast<FunctionParameters*>(params);

std::string name = (*args)[0];

TFunctor* func = IConsole::interface()->getCommand(name);

if(func != 0)
func->Call(args);
else
IConsole::interface()->add("Function Not Found");
}

The code works well from inside the IConsole class but but i need to pull it out so that i can eventually run it as a pthread. If I pass the pointer to IConsole to the function like so:

void executer(void* params, IConsole* con)
{
FunctionParameters* args = static_cast<FunctionParameters*>(params);

std::string name = (*args)[0];

TFunctor* func = con->getCommand(name);

if(func != 0)
func->Call(args);
else
con->add("Function Not Found");
}

everything works perfectly, except i can't use this to start a pthread. The IConsole->interface() call is used excessively throughout the many classes in the program without any trouble but always from inside a class. Is there some trick to calling the static class member function from a non-member class.

Any Ideas greatly appreciated.

visharad
July 18th, 2008, 04:10 AM
What compiler errors and in which lines are you getting?

Graham
July 18th, 2008, 04:21 AM
is IConsole::interface a public function?

Please give full details of your problem, including the text of the compiler error and the line of code that it objects to. A small, compilable sample that generates the error would be best of all.

As I told someone else, I've had to turn my telepathy off recently, because it was using too much energy.

muse1987
July 18th, 2008, 04:25 AM
I'm not sure whether I understand your problem correctly.

everything works perfectly, except i can't use this to start a pthread.
What does it mean? Is it because the function has 2 parameters, thus its signature is different than the one which library function expects? If that's the case, you could make a structure
struct SConsoleAndParam
{
void* params;
IConsole* con;
};

void executer(void* pConsoleAndParam)
{
void* params = reinterpret_cast<SConsoleAndParam*>(pConsoleAndParam)->params;
IConsole* con = reinterpret_cast<SConsoleAndParam*>(pConsoleAndParam)->con;

FunctionParameters* args = static_cast<FunctionParameters*>(params);

std::string name = (*args)[0];

TFunctor* func = con->getCommand(name);

if(func != 0)
func->Call(args);
else
con->add("Function Not Found");

free( pConsoleAndParam ); // assume structure is allocated on heap
}


You can allocate the structure on the heap before creating thread, set values to its members, and pass its address.

djwhy
July 18th, 2008, 08:53 AM
Thanks for the replies so far. Sorry i didn't post the error message I just couldn't stand looking at it any more. The error is:

expected unqualified-id before "struct"

And it appears anywhere that I use IConsole::interface() within the function.
As I understand it this means there is something wrong with the way I'm using the IConsole class.

I Just tried to put together some a code that would illustrate my point but it compiled so there must be something else wrong. Perhaps the way I'm using the singleton template or the order I'm defining everything.

I think for now I'm leaning to something like muses idea and come back to it later.

Peter_APIIT
July 19th, 2008, 09:38 PM
I didn't see you call a static member function from non member function.

s_kannan55
July 31st, 2008, 08:34 AM
(*args) gives you the value of the zeroth item of the array. In general
*(args+i) gives you the (i+1)th element of the array. I don't know why you have written (*args)[0] . Try to fix this and then get back.