Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ and serialization: is there any way to do some kind of introspection?

I've read some examples at Wikipedia but I'm looking for some real-life examples: how is introspection used, why (does it help to write clean code) and the code itself.

For example, is there a way to create a "generic" function to serialize any kind object? I mean: only one function in the parent, and all the descendent could be able to "save" + "restore" themselves into/from a file.

like image 658
Olivier Pons Avatar asked Nov 30 '25 21:11

Olivier Pons


2 Answers

It isn't used (since it doesn't exist), there's no code examples (since it doesn't exist), and there's no reason to attempt to use it (since it doesn't exist).

The closest you can get is RTTI/dynamic_cast. But that's not really introspection.

like image 99
Puppy Avatar answered Dec 03 '25 12:12

Puppy


There's RTTI in C++, and you can also use conditionals and dynamic_cast<>() to this effect, but in C++ we commonly strive to do as much as we can at compile time. If you feel a need for introspection chances are there are Better(tm) ways to achieve what you're aiming at with a static approach.

like image 31
wilhelmtell Avatar answered Dec 03 '25 13:12

wilhelmtell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!