Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

反射库不支持派生类 #318

Open
wangwenx190 opened this issue Oct 18, 2024 · 4 comments
Open

反射库不支持派生类 #318

wangwenx190 opened this issue Oct 18, 2024 · 4 comments

Comments

@wangwenx190
Copy link

struct MyStruct { int a = 1; };
struct MyStruct2 : public MyStruct { int b = 2; };
MyStruct2 data{};
ylt::reflection::for_each(data, [&](auto& value, auto name) {
    std::cout << name << ": " << value << std::endl;
});

这种情景编译都无法通过。就算对MyStructMyStruct2都加上YLT_REFL宏,也要求派生类那边的宏把基类的成员也都加上才行。有没有稍微简单点的办法来支持派生类呢

@qicosmos
Copy link
Owner

建议用组合,不要用继承, 类似于这样。

struct MyStruct {
};

struct MyStruct2 {
  MyStruct t;  
}; 

@wangwenx190
Copy link
Author

好的,非常感谢您的快速回复!目前我也打算采取这种方案了,也能达成我的目的。不过我还是想问下,继承的支持是不好做吗,有没有安排进roadmap呢

@qicosmos
Copy link
Owner

继承处理有点麻烦,后续会搞一个方案,优先级没那么高。

@wangwenx190
Copy link
Author

好的,明白了,感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants