How to use C ++ smart pointer up and down

two clients interact with each other on top of the message layer

in the body of the message, I need to include a field indicating any data type

From client A, I send the field as shared_ptr<TYPEA> to the message level. I define this field as shared_ptr<void> in the message layer. But how can I convert this field back to shared_ptr<TYPEA> in client B?

Or should I define shared_ptr<void> in the message layer as something else?

thanks

+4
source share
3 answers

If you use boost :: shared_ptr, you can use various functions XXX_ptr_cast <> () (static_ptr_cast, dynamic_ptr_cast ...).

If you are using MSVC 2010, I could not find an implementation of these functions. They cannot be part of the standard.

+2
source

If shared_ptrs and pointer data are not stored in memory shared by both clients (for example, clients work in different processes and data is not in shared memory), pointers from one client will not be valid for another. You will need to create a data view with pointers and pass them. The receiver creates its own copy of the data at the messaging level and passes shared_ptr before this to the client.

+2
source

If all the possible data types that you plan to transfer between clients are inherited from some common base class, you can simply include a flag variable in the base class that indicates which derived type is the current instance. Pass the base class pointers between clients, and then use dynamic_cast to downgrade the base class to the corresponding derived type.

0
source

Source: https://habr.com/ru/post/1307395/


All Articles