I'm currently deep-diving into the way pointers work.
Something for me unexplainable happened when executing the following lines of code:
std::vector<OptimizerPlanOperatorPtr> sources; for (const auto &source : sourceOperators){ OptimizerPlanOperator planOperator = OptimizerPlanOperator(source); sources.push_back(static_cast<std::shared_ptr<OptimizerPlanOperator>(&planOperator)); } all sourceOperators differ, however when checking the elements of sources, they all point to the same OptimizerPlanOperator.
When I ran the debugger, I realized that in every loop step, all values of sources change to the recent value.
My assumption is, that I poorly initialized the pointer here which somehow results in the value the pointer refers to being overridden.
Can somebody show a solution or explain, what I did wrong here?
OptimizerPlanOperatorPtr? What are you trying to do with thatstatic_castover there?OptimizerPlanOperator(source)probably should return astd::shared_ptr<OptimizerPlanOperator>so then you would haveauto planOperator = OptimizerPlanOperator(source);andsources.push_back(planOperator);and no dangling pointer or trying to free a stack variable