一道面试题细说C++类型转换

开篇先说这道面试题:

class ClassA
{
    public:
    virtual ~ ClassA()
    {
    }
    virtual void FunctionA()
    {
    }
};
class ClassB
{
    public:
    virtual void FunctionB()
    {
    }
};
class ClassC: public ClassA, public ClassB
{
    public:
};
ClassC aObject;
ClassA *pA = &aObject;
ClassB *pB = &aObject;
ClassC *pC = &aObject;

  

假设定义了ClassA* pA2,下面正确的代码是:
pA2=static_cast<ClassA*>(pB);
void* pVoid=static_cast<void*>(pB); pA2=static_cast<ClassA*>(pVoid);
pA2=pB;
pA2=static_cast<ClassA*>(static_cast<ClassC*>(pB));

//正确答案BD

  

posted @ 2015-08-10 19:46  stemon  阅读(344)  评论(0编辑  收藏  举报