Is Pointer-to- “ inner struct” member forbidden?

后端 未结 3 977
予麋鹿
予麋鹿 2020-11-29 09:05

i have a nested struct and i\'d like to have a pointer-to-member to one of the nested member:

is it legal?

struct InnerStruct
{
    bool c;
};
struct         


        
3条回答
  •  佛祖请我去吃肉
    2020-11-29 09:49

    The InnerStruct you care about happens to be contained in an instance of a MyStruct, but that doesn't affect how you get a pointer to the member of the InnerStruct.

    bool InnerStruct::* toto2 = &InnerStruct::c;
    

    Edit: Rereading your question, I'm guessing you want to define a pointer to member of the outer struct, and have it point directly at a member of the inner struct. That's simply not allowed. To get to the member of the inner struct that's contained in the outer struct, you'd have to create a pointer to the inner struct itselft, then to its member. To use it, you'd dereference both pointers to members:

    // Pointer to inner member of MyStruct:
    InnerStruct MyStruct::* toto = &MyStruct::inner;
    
    // Pointer to c member of InnerStruct:
    bool InnerStruct::* toto2 = &InnerStruct::c;
    
    // Dereference both to get to the actual bool:
    bool x = mystruct.*toto.*toto2;
    

提交回复
热议问题