When manually calling the second()
function I get a segmentation error. I think the problem is that along the way the program loses info dealing with where the first()
function is in the memory for the constructed D
class when moving the adress forward with +1
in int (*pfunSecond)(int) = (int (*)(int)) *(((void***)pb)[0] + 1);
. I haven't had luck fixing it so far.
#include <iostream>
class B{
public:
virtual int __cdecl first()=0;
virtual int __cdecl second(int)=0;
};
class D: public B{
public:
virtual int __cdecl first(){return 42;}
virtual int __cdecl second(int x){return first()+x;}
};
void myFunc(B* pb){
int (*pfunFirst)() = (int (*)()) *(((void***)pb)[0]);
int (*pfunSecond)(int) = (int (*)(int)) *(((void***)pb)[0] + 1);
std::cout << "Call first(): " << pfunFirst() << std::endl;
std::cout << "Call second(): " << pfunSecond(20) << std::endl;
}
int main(void){
myFunc(new D());
return 0;
}