Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 #include "OPTICKS_LOG.hh"
0002 
0003 struct Base
0004 {
0005    Base(int base_ ) 
0006       :
0007       base(base_)
0008    {
0009    }
0010 
0011    virtual void dump(const char* msg) = 0 ; 
0012    int base ; 
0013 
0014    static void Dump(Base* obj, const char* msg)
0015    {
0016        if(obj)
0017        {
0018            obj->dump(msg) ; 
0019        }
0020        else
0021        {
0022            LOG(info) << " NULL " << msg ; 
0023        }
0024    }
0025 
0026 };
0027 
0028 struct A : public Base 
0029 {
0030 
0031    A(int a0_) 
0032       :
0033       Base(100),
0034       a0(a0_)
0035    {
0036    }  
0037 
0038    int a0 ;
0039 
0040    void dump(const char* msg)
0041    {
0042        LOG(info) 
0043           << msg 
0044           << " Base.base " << base 
0045           << " A.a0 " << a0
0046           ; 
0047    }
0048 
0049 };
0050 
0051 struct B : public Base 
0052 {
0053    B(int b0_) 
0054       :
0055       Base(200),
0056       b0(b0_)
0057    {
0058    }  
0059 
0060    void dump(const char* msg)
0061    {
0062        LOG(info) 
0063           << msg 
0064           << " Base.base " << base 
0065           << " B.b0 " << b0
0066           ; 
0067    }
0068 
0069    int b0 ;
0070 };
0071 
0072 
0073 
0074 void test_cast()
0075 {
0076     Base* x = new A(42) ; 
0077 
0078 
0079     A* a_dc = dynamic_cast<A*>(x) ;  
0080     Base::Dump(a_dc, "a_dc");  
0081 
0082     A* a_rc = reinterpret_cast<A*>(x) ;   
0083     Base::Dump(a_rc, "a_rc");  
0084 
0085     A* a_sc = static_cast<A*>(x) ;   
0086     Base::Dump(a_sc, "a_sc");  
0087 
0088 
0089 
0090     B* b_dc = dynamic_cast<B*>(x) ;    // <--- only this one notices are casting to the wrong type and gives NULL
0091     Base::Dump(b_dc, "b_dc");  
0092 
0093     B* b_rc = reinterpret_cast<B*>(x) ;   
0094     Base::Dump(b_rc, "b_rc");  
0095 
0096     B* b_sc = static_cast<B*>(x) ;   
0097     Base::Dump(b_sc, "b_sc");  
0098 }
0099 
0100 
0101 
0102 void test_cast_2()
0103 {
0104      int a,b,c,d;
0105      char h = 1;
0106      short s = 1;
0107      void* p = nullptr;
0108      int i = 1;
0109 
0110      assert( sizeof(long) == 8 ); 
0111 
0112      std::cout << "a is at: " << reinterpret_cast<long>(&a) << std::endl;
0113      std::cout << "b is at: " << reinterpret_cast<long>(&b) << std::endl;
0114      std::cout << "c is at: " << reinterpret_cast<long>(&c) << std::endl;
0115      std::cout << "d is at: " << reinterpret_cast<long>(&d) << std::endl;
0116      std::cout << "Char is at: " << reinterpret_cast<long>(&h) << std::endl;
0117      std::cout << "Short is at: " << reinterpret_cast<long>(&s) << std::endl;
0118      std::cout << "Pointer is at: " << reinterpret_cast<long>(p) << std::endl;
0119      std::cout << "Int is at: " << reinterpret_cast<long>(&i) << std::endl;
0120 }
0121 
0122 
0123 int main(int argc, char** argv)
0124 {
0125     OPTICKS_LOG(argc, argv); 
0126 
0127     //test_cast(); 
0128   
0129     test_cast_2(); 
0130 
0131     return 0 ;
0132 }
0133 
0134 //om- ; TEST=CastTest om-t 
0135 
0136