zoukankan      html  css  js  c++  java
  • Note of using static_cast

    #include "stdafx.h"
    #include <assert.h>

    class IInterface1
    {
    public:
     virtual void VFunc1() = 0;
    };

    class IInterface2
    {
    public:
     virtual void VFunc2() = 0;
    };

    class TestClass1:
     public IInterface1, public IInterface2

    {
     void VFunc1();
     void VFunc2();
    };

    void TestClass1::VFunc1()
    {
     ;
    }
    void TestClass1::VFunc2()
    {
     ;
    }

    class Interface12: 
     public IInterface1, public IInterface2
    {

    };
    template<class Intttt>
    Intttt* MakeInterface2(IInterface1* pInterface1)
    {
     return static_cast<Intttt*>( static_cast<Interface12>(pInterface1) );
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
     TestClass1* pTestClass1 = new TestClass1();
     IInterface1* pInterface1 = dynamic_cast<IInterface1*>(pTestClass1);
     assert(pInterface1);
     IInterface2* pInterface2 = static_cast<IInterface2*>(pInterface1);
     IInterface2* pInterface2 = MakeInterface2<IInterface2>(pInterface1);
     return 0;
    }

    The red line code will cause compile error:
    error C2440: 'static_cast' : cannot convert from 'IInterface1 *' to 'IInterface2 *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

  • 相关阅读:
    CSS同时选择器
    create-react-app之proxy
    mysql limit语句
    给tbody加垂直滚动条的具体思路
    MySql数据类型范围
    block、inline、inline-block
    javascript sourcemap
    session of express
    React中innerHTML的坑
    box-sizing
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1086203.html
Copyright © 2011-2022 走看看