A confusing typedef involves class scope

前端 未结 2 501
既然无缘
既然无缘 2021-02-01 01:07

I\'m reading code of a C++ project and it contains some code of the following form:

namespace ns {
    class A {};
    class B {};
}

struct C {
    typedef ns::         


        
2条回答
  •  囚心锁ツ
    2021-02-01 01:26

    The answer by @vsoftco already answers the core of the question. This answer shows how one might use such a typedef.

    #include 
    #include 
    
    namespace ns {
    
       struct A {};
    
       struct B
       {
          A* a1;
          A* a2;
       };
    }
    
    struct C {
       typedef ns::A* ns::B::*type;
    };
    
    int main()
    {
       C::type ptr1 = &ns::B::a1;
       C::type ptr2 = &ns::B::a2;
    
       ns::B b1;
       b1.*ptr1 = new ns::A; // Samething as b1.a1 = new ns::A;
    
       return 0;
    }
    

提交回复
热议问题