Open DapengFeng opened 1 week ago
I suppose you could try to write a custom parser (refer to the documentation).
But I think the bigger question is: Why do you want to do this?
I don't want to implement the custom parser for the derived struct just because I'm lazy:)
OK, fair enough, but the question is what are you trying to do in the first place? Personally, I can't think of any good reason to inherit from rfl::Field...
rfl::Field
is just an example. I want to define a derived class from the base one without implementing the custom parser.
I am a junior at reflect-cpp
, and I want to learn it in depth. Do you have any advice? Hope to hear you :) @liuzicheng1987
I am a junior at
reflect-cpp
, and I want to learn it in depth. Do you have any advice? Hope to hear you :) @liuzicheng1987
Especially the logic in the internal namespace.
I don't want to implement the custom parser for the derived struct just because I'm lazy:)
A Tip:
option(REFLECT_CPP_C_ARRAYS_OR_INHERITANCE "Enable C arrays and inheritance" ON)
Hi @DapengFeng,
inheritance is a bit of a tricky topic. We use structured bindings to automatically access the fields and so far no one has thought of a way to do this with inheritance.
You might also want to take a look at rfl::Flatten. Maybe that would solve your problem.
Thank you for your advice. I try to learn reflect-cpp
.
Can I use the operator[] to access the named field in the runtime?
@DapengFeng , sure:
https://github.com/getml/reflect-cpp/blob/main/docs/generic.md
You could try something like:
const auto obj = rfl::to_generic(my_struct).to_object().value();
obj["my_field"];
If I define a struct derived from the base struct
rfl::Field,
how can I implement the parser for the new struct?