c++——dynamic_cast的使用

1)dynamic_cast要求操作数必须是多态类型(即要求父类要存在virtual虚函数);

class Base
{
public:
    Base() {

    }

    ~Base() {

    }

    void test()
    {
        cout << "Base" << endl;
    }
};

class Derived : public Base
{
public:
    Derived()
    {

    }

    ~Derived()
    {

    }

    void test()
    {
        cout << "Derived" << endl;
    }
};

int main()
{
    Base* pB = new Derived();

    Derived* pD = dynamic_cast(pB);  // error: dynamic_cast要求操作数必须是多态类型

    system("pause");
    return 0;
}

2)单继承情况,可以判断dynamic_cast强转后的指针是否为空判断是否转换成功;

class Base
{
public:
    Base() {

    }

    ~Base() {

    }

    virtual void test()
    {
        cout << "Base" << endl;
    }
};

class Derived : public Base
{
public:
    Derived()
    {

    }

    ~Derived()
    {

    }

    virtual void test()
    {
        cout << "Derived" << endl;
    }
};

int main()
{
    // 1. 单继承  pD指向的是Derived
    Base* pB = new Derived();
    Derived* pD = dynamic_cast(pB);
    if (nullptr == pD)
        cout << "pD is nullptr!" << endl;

    // 2. 单继承 pD1不是指向Derived 此时dynamic_cast强转后返回nullptr
    Base* pB1 = new Base();
    Derived* pD1 = dynamic_cast(pB1);  
    if (nullptr == pD1)
        cout << "pD1 is nullptr!" << endl;

    system("pause");
    return 0;
}

3)多继承;

class Base
{
public:
    Base() {

    }

    ~Base() {

    }

    virtual void test()
    {
        cout << "Base" << endl;
    }
};

class Base1
{
public:
    Base1() {

    }

    ~Base1() {

    }

    virtual void test1()
    {
        cout << "Base1" << endl;
    }
};

class Derived : public Base, public Base1
{
public:
    Derived()
    {

    }

    ~Derived()
    {

    }

    virtual void test()
    {
        cout << "Derived" << endl;
    }

    virtual void test1()
    {
        cout << "Derived-1" << endl;
    }
};

int main()
{
    // 1. 多继承  pD指向的是Derived
    Base* pB = new Derived();
    Derived* pD = dynamic_cast(pB);
    if (nullptr == pD)
        cout << "pD is nullptr!" << endl;

    // 2. 多继承 pD1指向的是Derived
    Base1* pD1 = dynamic_cast(pB);
    if (nullptr == pD1)
        cout << "pD1 is nullptr!" << endl;

    pD1->test1();  // 打印 Derived-1

    system("pause");
    return 0;
}

4)基类强转为派生类,此种方式无效,dynamic_cast返回的指针为nullptr;

class Base
{
public:
    Base() {

    }

    ~Base() {

    }

    virtual void test()
    {
        cout << "Base" << endl;
    }
};

class Base1
{
public:
    Base1() {

    }

    ~Base1() {

    }

    virtual void test1()
    {
        cout << "Base1" << endl;
    }
};

class Derived : public Base, public Base1
{
public:
    Derived()
    {

    }

    ~Derived()
    {

    }

    virtual void test()
    {
        cout << "Derived" << endl;
    }

    virtual void test1()
    {
        cout << "Derived-1" << endl;
    }
};

int main()
{
    Base* pB1 = new Base();
    Derived* pD2 = dynamic_cast(pB1);  // 转换无效 pD2为nullptr
    if (nullptr == pD2)
        cout << "pD2 is nullptr!" << endl;

    system("pause");
    return 0;
}

你可能感兴趣的:(c++,c++)