草庐IT

c++ - 返回 shared_ptr 时如何实现协变返回类型?

usingnamespaceboost;classA{};classB:publicA{};classX{virtualshared_ptrfoo();};classY:publicX{virtualshared_ptrfoo();};返回类型不是协变的(因此它们也不是合法的),但如果我使用原始指针代替它们就会是。解决此问题的常用习语是什么(如果有的话)? 最佳答案 我认为解决方案基本上是不可能的,因为协方差取决于与智能指针不兼容的指针算法。当Y::foo返回shared_ptr对于动态调用者,它必须转换为shared_ptr使用前

c++ - 返回 shared_ptr 时如何实现协变返回类型?

usingnamespaceboost;classA{};classB:publicA{};classX{virtualshared_ptrfoo();};classY:publicX{virtualshared_ptrfoo();};返回类型不是协变的(因此它们也不是合法的),但如果我使用原始指针代替它们就会是。解决此问题的常用习语是什么(如果有的话)? 最佳答案 我认为解决方案基本上是不可能的,因为协方差取决于与智能指针不兼容的指针算法。当Y::foo返回shared_ptr对于动态调用者,它必须转换为shared_ptr使用前

c++ - 使用 gdb 检查 boost shared_ptr

以下是我的源代码:#include#includeclassMyClass{public:MyClass(){i=10;}private:inti;};intmain(intargc,constchar*argv[]){boost::shared_ptrobj(newMyClass());return0;}我想查看gdb中的obj,查看成员变量i的值。这是我用普通打印得到的:29boost::shared_ptrobj(newMyClass());(gdb)n30return0;(gdb)pobj$1={px=0x602010,pn={pi_=0x602030}}我尝试了thislin

c++ - 使用 gdb 检查 boost shared_ptr

以下是我的源代码:#include#includeclassMyClass{public:MyClass(){i=10;}private:inti;};intmain(intargc,constchar*argv[]){boost::shared_ptrobj(newMyClass());return0;}我想查看gdb中的obj,查看成员变量i的值。这是我用普通打印得到的:29boost::shared_ptrobj(newMyClass());(gdb)n30return0;(gdb)pobj$1={px=0x602010,pn={pi_=0x602030}}我尝试了thislin

c++ - unique_ptr 编译错误

如果我告诉你我无法编译它,我想这会很尴尬。你能帮帮我吗:#includeusingnamespacestd;intmain(){std::unique_ptrp1(newint(5));return0;}$gccmain.cppmain.cpp:Infunction‘intmain()’:main.cpp:6:2:error:‘unique_ptr’wasnotdeclaredinthisscopemain.cpp:6:13:error:expectedprimary-expressionbefore‘int’main.cpp:6:13:error:expected‘;’before‘

c++ - unique_ptr 编译错误

如果我告诉你我无法编译它,我想这会很尴尬。你能帮帮我吗:#includeusingnamespacestd;intmain(){std::unique_ptrp1(newint(5));return0;}$gccmain.cppmain.cpp:Infunction‘intmain()’:main.cpp:6:2:error:‘unique_ptr’wasnotdeclaredinthisscopemain.cpp:6:13:error:expectedprimary-expressionbefore‘int’main.cpp:6:13:error:expected‘;’before‘

c++ - 惯用 std::auto_ptr 还是只使用 shared_ptr?

既然shared_ptr在tr1中,你认为std::auto_ptr的使用会发生什么?它们都有不同的用例,但auto_ptr的所有用例也都可以用shared_ptr解决。你会放弃auto_ptr还是在你想明确表示在任何给定点只有一个类拥有所有权的情况下继续使用它?我的看法是,使用auto_ptr可以增加代码的清晰度,正是通过添加细微差别和代码设计的指示,但另一方面,它在训练时增加了另一个微妙的问题新程序员:他们需要了解智能指针以及它们如何工作的细节。当您在任何地方都只使用一个智能指针时,您只需制定一条规则“将所有指针包装在shared_ptr中”并完成它。您对此有何看法?

c++ - 惯用 std::auto_ptr 还是只使用 shared_ptr?

既然shared_ptr在tr1中,你认为std::auto_ptr的使用会发生什么?它们都有不同的用例,但auto_ptr的所有用例也都可以用shared_ptr解决。你会放弃auto_ptr还是在你想明确表示在任何给定点只有一个类拥有所有权的情况下继续使用它?我的看法是,使用auto_ptr可以增加代码的清晰度,正是通过添加细微差别和代码设计的指示,但另一方面,它在训练时增加了另一个微妙的问题新程序员:他们需要了解智能指针以及它们如何工作的细节。当您在任何地方都只使用一个智能指针时,您只需制定一条规则“将所有指针包装在shared_ptr中”并完成它。您对此有何看法?

c++ - 非数组类型的 "one-past-the-end"指针是 C++ 中的有效概念吗?

C++标准[sec5.7]说:Ifboththepointeroperandandtheresultpointtoelementsofthesamearrayobject,oronepastthelastelementofthearrayobject,theevaluationshallnotproduceanoverflow;otherwise,thebehaviorisundefined.那么,我是否正确地假设除了数组之外的其他类型的指针是未定义的?例如:inta=0;vectorv(&a,(&a)+1);上面的代码片段编译和工作得很好(使用g++),但它有效吗?

c++ - 非数组类型的 "one-past-the-end"指针是 C++ 中的有效概念吗?

C++标准[sec5.7]说:Ifboththepointeroperandandtheresultpointtoelementsofthesamearrayobject,oronepastthelastelementofthearrayobject,theevaluationshallnotproduceanoverflow;otherwise,thebehaviorisundefined.那么,我是否正确地假设除了数组之外的其他类型的指针是未定义的?例如:inta=0;vectorv(&a,(&a)+1);上面的代码片段编译和工作得很好(使用g++),但它有效吗?