Question about std:any and std::shared_ptr types

Sorry that this is not an Unreal-specific question, but just related to C++. Before recently starting to learn Unreal, I haven’t programmed in C++ for probably 15-20 years (mainly using C#, VB.NET, python, etc. in the meantime). I used to be fairly proficient in C++ way back then, but it seems that since that time a number of “standard” types were added to C++ to try to emulate some of nicer features of C# (garbage collection, “object” type, lists & collections, etc.). I’m trying to understand how some of these work.

Regarding the “any” type. As I understand it’s trying to be similar to the C# “object” base type functionality in a way that’s better than just using void*.
Based on the code snippet below, I have a few questions:

   class c_class { public: int b;};
   c_class d = *(new c_class());
   d.b = 5;
   std::any a = d;
   std::any a2 = a;
   std::any_cast<c_class>(a2).b = 10;

Is the variable “a” storing a pointer to “d” or a copy of “d”? I believe it’s a pointer, but couldn’t find a clear answer to that.
When variable “a” is assigned to “a2”, do they both now refer to the same address? So when I cast a2 to “c_class” and set “b” field to 10, would it change the original instance “d” as well?

Regarding std::shared_ptr. My understanding is this pointer keeps track of references to the object it points to and deletes the object when all references are destroyed. What happens if, in addition to having a few shared_ptr’s point to this object, I also have some standard pointers point to it. Would the object get deleted when no more share_ptrs’ point to it, making any still existing standard pointers to it invalid?

Thanks a lot for your help. Again, sorry that these questions are not Unreal specific.

To answer your questions one by one;

  • d becomes an instance in this instance (pun intended :stuck_out_tongue_winking_eye: )

  • a2 and a don’t point at the same address, which makes sense considering we already cleared that d is in fact a copy

  • No it doesn’t change d instance, which also makes sense after we see that a and a2 don’t point at the same location, reminding us that a2 is a seperate copy

  • Yes that’s right, since we’ve established that these are copies that exist thanks to pointers, they would be gone if no pointer points at them, which was again, the whole meaning of their lives :cold_sweat: yeah those copies must be shivering their timbers

Also, you can prove these by using a bunch of couts and see it yourself! :smiling_face_with_three_hearts: