|
-
December 16th, 2011, 05:15 PM
#3
Re: Variadic function pointer
Visual Studio 2010 c++ compiler:
error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(void *,int,int)' to 'void (__cdecl *)(void *,...)'
1> None of the functions with this name in scope match the target type
Gcc 4.7.0:
error: invalid conversion from 'void (*)(void*, int, int)' to 'void (*)(void*, ...)' [-fpermissive]|
 Originally Posted by MrViggy
your free standing function has the wrong type to be assigned to the member 'ptr', as it should be expecting an implicit 'this' pointer
This is incorrect since he only defined and declared a free standing function pointer, so you can assign any free standing function that matches the same prototype. For example this will compile:
Code:
#include <iostream>
using namespace std;
struct Test
{
void (*ptr)(void* p);
};
void myfunction(void* p) {}
int main()
{
Test test = { &myfunction };
return 0;
}
The typical this pointer problem people run into is trying to get c to call a c++ class member like:
Code:
#include <iostream>
using namespace std;
struct Test
{
void (*ptr)(void* p);
};
class MyClass
{
public:
void myfunction(void* p) {}
};
int main()
{
MyClass Class;
Test test = { &Class.myfunction };
return 0;
}
This will complain about the missing the class pointer
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|