Re: Casting function pointers





KKL wrote:

I wonder how casting with function pointers work, how it "should be"
handled and how it "should not be" handled. Can anyone tell whether
the below code should work or not?

Module: Task Service Abstraction Layer
int taskCreate(char* taskName, void* funcPtr, int
taskPriority,..........)
{
myTaskStructure TaskProperties;
int TaskId, errStatus;

............
//Casting void* funcPtr to function pointer that returns void and
accepts no arguments
TaskProperties.entryPtr = (void ()(void)* ) funcPtr;
errStatus = rtosTaskCreate( taskName, &TaskProperties, &TaskId);

.........
}

Module: SomeClass.cpp

void someClass::taskLoop( int taskArgument)
{
.......
}

void somClass::initialize( void )
{

taskCreate( "Task1", (void* )&taskLoop, 25,.....);
}

Module: SomeOtherClass.cpp


My question is whether these kind of casting for function pointers is
valid.

No it is not. There is plenty of mistakes in your example. The first one: you can't cast a class member function to an ordinary function.


Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com



.



Relevant Pages