-3
我有3个类A,B和C. C是从A和B派生的。我得到指向C类指针的指针并将其转换为A **,并且B * *,即保持了B中的变量**有**的在我的例子B中的地址** BdoublePtr持有A的地址**。我现在用的是下面的代码
多重继承中派生类的指针指向
#include "conio.h"
#include "stdio.h"
#include "string.h"
class A
{
public:
A()
{
strA=new char[30];
strcpy(strA,"class A");
}
char *strA;
};
class B
{
public:
B()
{
strB=new char[30];
strcpy(strB,"class B");
}
char *strB;
};
class C : public A, public B
{
public:
C()
{
strC=new char[30];
strcpy(strC,"class C");
}
char *strC;
};
int main(void)
{
C* ptrC=new C();
A * Aptr=(A*)ptrC;
printf("\n class A value : %s",Aptr->strA);
B * Bptr=(B*)ptrC;
printf("\n class B value :%s",Bptr->strB);
printf("\n\nnow with double pointer ");
A ** AdoublePtr=(A **)&ptrC;
Aptr=*AdoublePtr;
printf("\n class A value : %s",Aptr->strA);
B * * BdoublePtr=(B **)&ptrC;
Bptr=* BdoublePtr;
printf("\n class B value : %s",Bptr->strB);
getch();
return 0;
}
你可以发布你的代码没有额外的HTML标记,并缩进它所有的四个空格或按代码按钮,使其显示格式? – 2010-06-18 10:11:18
在您的问题中提出问题。 – 2010-06-18 10:11:31
我在这里没有看到任何问题,但是将C风格演员与多重继承结合使用几乎肯定会出错。改为使用'static_cast'或'dynamic_cast'。 – 2010-06-18 10:14:26