Skip to main content
36 events
when toggle format what by license comment
Oct 30 at 1:50 vote accept galaxied
Oct 29 at 21:06 history edited galaxied CC BY-SA 4.0
added 542 characters in body
Oct 29 at 5:20 answer added n. m. could be an AI timeline score: 0
Oct 29 at 4:55 comment added n. m. could be an AI My advice is to make all non-leaf classes abstract. This way you will immediately realize that auto BaseToken = std::make_shared<BaseStruct>(); is a mistake: it won't compile.
Oct 29 at 4:49 comment added JaMiT "To be fair the example didn't use [...]" -- Which example?
Oct 29 at 4:45 comment added JaMiT "I have my base struct that contains values as a shared_ptr" -- This does not match your code. I read this as saying you have a base struct, let's call it BaseStruct, that has a shared_ptr member to the values it contains. Your code indicates that instead you have a shared_ptr to the base struct. This would be "I have a shared_ptr to my base struct that contains values". And "values" reads strange. Not exactly wrong, but try "fields" or "data members" or "some data".
Oct 29 at 4:41 comment added JaMiT This question is a difficult read. After re-reading it a few times, I think I understand what you might be trying to say, but a Stack Overflow question should not be that hard to understand. One good example might be "I have to downcast this base struct to assign more values." This basically assumes that the reader knows what your goal is (which is a bad assumption), focusing instead on how you're getting to that goal. At a guess, this one sentence should be expanded into a full paragraph that explains what's going on. I'd also put it before the code (cf. How to Ask).
Oct 29 at 4:06 comment added Jerry Jeremiah I would do it like this: onlinegdb.com/epPCrWGdHM If all your derived classes have a constructor that takes a BaseStruct shared_ptr and you BaseStruct has a constructor that takes a BaseStruct shared_ptr you can pass the previous derived class as a parameter to the make_shared function.
Oct 29 at 3:23 comment added Avi Berger Your choice. 1) Start by creating and object of the final derived type, even if you initially access it via a pointer to base type; or 2) Start with a base object, later create a different derived object, copy over any values you want from the base object , and redirect the pointer from the old object to the totally separate new object you want to use in its place. You can't use a derived object unless you have that derived object. Downcasting does not create or convert an object, it only works if the object is actually already of that type, but you were accessing it via a base pointer.
Oct 29 at 3:22 comment added Passer By A family of types is not a single type. A base class will not transform into a derived class no matter how you ask it. When you create a base class, you don't have a derived class. You can either copy the values you want over or you can wait until you know what derived class you want before creating an object.
Oct 29 at 3:15 comment added Avi Berger To create it, you must know the specific derived type. You can't start with a base and later expand it. You can redirect the pointer to a different object later, but there will be no carry over of values unless you specifically copy/move over the values as it is a different, independent object.
Oct 29 at 3:10 comment added galaxied @AviBerger How would I declare the derived pointer inside the base struct. It can’t be a specific derived struct since there are multiple derived structs.
Oct 29 at 3:10 comment added Avi Berger Start with std::shared_ptr<BaseStruct> BaseToken = std::make_shared<Dervied_T>(); to create derived object pointed to by base pointer. auto DerivedToken = std::static_pointer_cast<DerivedStruct> (BaseToken); to get derived pointer later.
Oct 29 at 3:07 comment added galaxied @AviBerger I keep the same pointer since other parts of the program need to access that specific struct.
Oct 29 at 3:05 comment added galaxied @StephenNewell yes I know that it replaces the original, but if I remove that and try to downcast directly with static_pointer_cast, the base members are kept but the derived members aren’t accessible.
Oct 29 at 3:04 comment added Avi Berger What you are doing in your revised post is discarding the original base object and reusing the pointer you had pointing to it to point to an entirely new, default initialized object. There is no carry over of values as you are tossing the old object entirely and starting fresh with a new one. Start by creating the derived object, work with it, if desired, via a pointer to base and, if desired even later, via a pointer to derived.
Oct 29 at 3:02 comment added Stephen Newell BaseToken = std::make_shared<AnotherDerivedStruct>(); You realize this completely replaces the original BaseToken value, right? If you add ctors and a dtor with some logging, you can watch object lifetimes.
Oct 29 at 2:59 comment added Avi Berger "I have to downcast this base struct to assign more values." Are you aware you cannot downcast an actual base struct to a derived one? You can downcast from a pointer to a base struct to a pointer to a derived one only if the base pointer is actually already pointing to a existing derived instance.
Oct 29 at 2:57 history edited galaxied CC BY-SA 4.0
added 8 characters in body
Oct 29 at 2:57 history edited galaxied CC BY-SA 4.0
added 31 characters in body
Oct 29 at 2:52 comment added galaxied @StephenNewell You can test it locally now.
Oct 29 at 2:52 history edited galaxied CC BY-SA 4.0
added 42 characters in body
Oct 29 at 2:50 history edited galaxied CC BY-SA 4.0
added 70 characters in body
Oct 29 at 2:49 history edited galaxied CC BY-SA 4.0
fixed errors
Oct 29 at 2:46 history edited galaxied CC BY-SA 4.0
deleted 1 character in body
Oct 29 at 2:46 review Close votes
Nov 2 at 0:01
Oct 29 at 2:44 history edited galaxied CC BY-SA 4.0
added 65 characters in body
Oct 29 at 2:43 history edited galaxied CC BY-SA 4.0
added 58 characters in body
Oct 29 at 2:40 history edited galaxied CC BY-SA 4.0
added 427 characters in body
Oct 29 at 2:40 comment added Stephen Newell This still isn't a minimal reproducible example. What's enum_T? Give us something we can copy/paste without modification.
Oct 29 at 2:34 history edited galaxied CC BY-SA 4.0
added 168 characters in body
Oct 29 at 2:29 comment added Avi Berger BaseToken = std::make_shared<Dervied_T>(BaseToken); looks wrong to me. Is this a typo in your post? I'd expect std::shared_ptr<BaseStruct> BaseToken = std::make_shared<Dervied_T>(); And no, you shouldn't scrap shared pointer for raw.
Oct 29 at 2:28 comment added Passer By Please provide a minimal reproducible example.
Oct 29 at 2:11 history edited galaxied CC BY-SA 4.0
added 51 characters in body
S Oct 29 at 2:10 review First questions
Oct 29 at 8:54
S Oct 29 at 2:10 history asked galaxied CC BY-SA 4.0 created from wizard