Can I use the method doSomething like this; first having assigned the pointer to the A class to the void* member of b?
class A
{
public:
A(int);
int m_x;
int doSomething(){};
};
class B
{
public:
void* m_y;
};
#include "x.h"
using namespace std;
A::A(int x)
{
m_x = x;
}
int main()
{
//create 2 pointers to A and B
B *b;
A *a;
b = new B();
a = new A(15);
b->m_y = a;
((A*)b->m_y)->doSomething();
delete a;
delete b;
return 0;
}
Stay away from
void*Why not have a pointer to
AinB(or even,Bowns the instance ofA), orBholds a smart pointer to an instance ofA– there are so many other better ways of doing things.