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

Derived Struct (Nested Struct) #239

Open
DapengFeng opened this issue Oct 30, 2024 · 11 comments
Open

Derived Struct (Nested Struct) #239

DapengFeng opened this issue Oct 30, 2024 · 11 comments

Comments

@DapengFeng
Copy link

If I define a struct derived from the base struct rfl::Field, how can I implement the parser for the new struct?

@liuzicheng1987
Copy link
Contributor

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?

@DapengFeng
Copy link
Author

I don't want to implement the custom parser for the derived struct just because I'm lazy:)

@liuzicheng1987
Copy link
Contributor

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...

@DapengFeng
Copy link
Author

rfl::Field is just an example. I want to define a derived class from the base one without implementing the custom parser.

@DapengFeng
Copy link
Author

DapengFeng commented Oct 30, 2024

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

@DapengFeng
Copy link
Author

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.

@DapengFeng
Copy link
Author

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)

@DapengFeng DapengFeng changed the title Where can I find the parser for rfl::Field? Derived Struct (Nested Struct) Nov 1, 2024
@liuzicheng1987
Copy link
Contributor

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.

@DapengFeng
Copy link
Author

Thank you for your advice. I try to learn reflect-cpp .

@DapengFeng
Copy link
Author

DapengFeng commented Nov 4, 2024

Can I use the operator[] to access the named field in the runtime?

@liuzicheng1987
Copy link
Contributor

liuzicheng1987 commented Nov 4, 2024

@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"];

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