Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting const void* pointer to specific class pointer

Tags:

c++

I have function declaration as below

void func1(const void& * pThis) {
    MyClass* pMyClass = static_cast<MyClass*>(pThis);    //....I use PMyClass pointer.
}

I am getting error cannot convert const void* to MyClass*

How to do this step?

like image 581
venkysmarty Avatar asked Jan 16 '23 04:01

venkysmarty


1 Answers

You can

MyClass* pMyClass = const_cast<MyClass*>( static_cast<const MyClass*>(pThis) );

But this awful syntax is the hint: why then the function has a const argument, wouldn't you want it to be like

void func1(void * pThis) {

Of course, you can take a shortcut using C-style cast:

MyClass* pMyClass = (MyClass*)pThis;

but I would fix the design instead if possible.

like image 127
unkulunkulu Avatar answered Jan 30 '23 23:01

unkulunkulu