C++11 访问对象拥有的数据集中共享的ptr的指针对象

C++11 访问对象拥有的数据集中共享的ptr的指针对象,c++11,reference,shared-ptr,deque,C++11,Reference,Shared Ptr,Deque,在C++11中,让一个对象拥有一组共享的_ptr。 要访问队列前端的指针对象属性和方法,我希望获得对a->deque.front的引用,但这似乎不像我预期的那样有效 下面是缩小问题范围的代码摘录:在使用副本或引用时,只显示通过shared_ptr get方法获得的指针对象的地址 auto const my_copy = A->deque().front(); cout<<"my_copy pointee: "<<hex<<reinterpret_cas

在C++11中,让一个对象拥有一组共享的_ptr。 要访问队列前端的指针对象属性和方法,我希望获得对a->deque.front的引用,但这似乎不像我预期的那样有效

下面是缩小问题范围的代码摘录:在使用副本或引用时,只显示通过shared_ptr get方法获得的指针对象的地址

auto const  my_copy = A->deque().front();
cout<<"my_copy pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_copy.get())<<endl;
auto const & my_ref = A->deque().front();
cout<<"my_ref  pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_ref .get())<<endl;

我的印象是,我真的错过了一些明显的东西…

代码似乎正确,最可能的罪魁祸首是A::deque,它可能按值返回。这将导致未定义的行为。代码与额外引用一起工作,因为您正在将临时引用的生存期延长到该引用的生存期。

只要您不处理char*,对uintptr\t的强制转换应该是不必要的。您确定A::deque是通过引用而不是偶然地通过值返回的吗?行为是否符合预期。问题一定出在其他地方。这就是我发现的:A->deque按值返回,作为成员deque的排序副本。编译器看起来也很好?对此事敏感,没有发出任何可能有帮助的警告。无论如何,谢谢。我把这个贴出来作为答案,所以你可以接受。绿色记号。
auto const  my_copy = A->deque().front();
cout<<"my_copy pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_copy.get())<<endl;
auto const & my_ref = A->deque().front();
cout<<"my_ref  pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_ref .get())<<endl;
auto const & my_deque = A->deque();
auto const  my_copy = my_deque.front();
cout<<"my_copy pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_copy.get())<<endl;
auto const & my_ref = my_deque.front();
cout<<"my_ref  pointee: "<<hex<<reinterpret_cast<uintptr_t>(my_ref .get())<<endl;
my_copy pointee: 7fa222c1be60
my_ref  pointee: 7fa222c1be60