c++ - Pointing a derived smart pointer to a stack object returned by base reference -
I have a problem. I want to point to those objects that are stored in the class. I code has one holder. The getSomething () function that gives context to a base, I want to put that derived B_PRR here is the code:
#include & lt; Memory & gt; using namespace std; Class A {Public: int a; A (): A (0) {} Virtual ~ A () {}}; Class B: Public A {public: bool; B (): a () {b = false; }}; Class holder {public: B ARE [1]; // It has a referee, no b, because I will load a bottle of derivative. & Amp; GetSomething () {Return arr [0]; } Holder () {arr [0] = B (); }}; Int main () {Holder H; Shared_ptr & lt; P & gt; B_ptr; //b_ptr = some_lines_Here (h.getSomething ()); Return 0; };
I know (and "know" I mean I have an uneducated guess) that I should use dynamic_ (pointer _?) Cast, but I know the correct syntax address You can not know / can not know the full point of a shared pointer is that its referee is counting and destroying - which tells it when the last chance goes out of the scope. You do not want to do that, The object of the second class is to A's, because he has undefined behavior.
In short; Do not do this.
Comments
Post a Comment