Reflection in C++

后端 未结 8 1846
失恋的感觉
失恋的感觉 2021-02-05 19:55

I\'ve been working for years with Java. During those years, I\'ve made extensive (or maybe just frequent) use of reflection, and found it useful and enjoyable. But 8 months ago

8条回答
  •  心在旅途
    2021-02-05 20:17

    You need to use the visitor pattern. Any class that can be reflected on would need to inherit a base class that gave it a Reflect member function, which would accept a visitor class. Then the Reflect function would pass information or capabilities about the other members to the visitor.

    Many popular libraries use this pattern for specific cases, for example, the Serialize function in MFC does this but specifically for serialization.

    You could probably design a system in such a way that the visitor would be able to make dynamic calls to member functions, get or set the values of data members, etc. But it would be the responsibility of each class to maintain a Reflect function written by hand, which would be a repetition of the structure of the class.

提交回复
热议问题