我可以使用像這樣的方法doSomething;首先將指向A類的指針賦給b的void *成員?這是使用void *成員指針的正確方法嗎?
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;
}
也許你可以,但爲什麼你應該扔掉打字並強迫自己投擲*無處不在*,基本上在你使用它的地方打開未定義行爲的可能性? – delnan