jjzjj

从基础到派生的 shared_ptr 的 C++ dynamic_ptr_cast 失败

这是我本周遇到的一个益智游戏。部分原因是我在编写了一段时间的Java代码后刚刚回到C++编码。给定以下代码:classBase{};classA:Base{public:virtualvoidrun(){coutptrToA=shared_ptr(newC());cout(ptrToA)run();assert(dynamic_pointer_cast(ptrToA));cout为什么会产生如下输出?PointertoA:0x1f29c010DynamicCastAptrtoC:0Running...ThisisC.tester-cpp:tester.cpp:89:intmain(in

c++ - const_cast 类构造函数中的 const 成员

当我希望类的成员变量在类的生命周期内保持不变,但在构造函数期间需要可变时,我有时会使用const_cast。示例:structqqq{constvectormy_foo;qqq(vector*other){vector&mutable_foo=const_cast&>(my_foo)other->swap(mutable_foo);}};我曾假设在构造函数中执行此操作基本上没问题,因为此时没有其他人依赖它,因此它不会与优化等产生不良交互。但是最近有人告诉我这是“未定义的行为”,并且在任何情况下在构造const对象之后对其进行变异基本上都是非法的。有人能解释一下吗?这是不好的/未定义的行

c++ - -Werror=old-style-cast 的意义?

我正在使用将一些整数转换为float以进行除法的代码。size_ta;uint8_tb,c;a=(float)b/(float)c;我在编译时启用了警告标志,我得到了一个用于“旧类型转换”的警告标志。有没有更好或更合适的方式来转换这些东西?如果是,怎么办? 最佳答案 旧式转换是“C风格”转换。-Werror=old-style-cast将C风格转换的使用变成错误。你应该使用C++casts.在这里你可以使用一个static_cast:size_ta;uint8_tb,c;a=static_cast(b)/static_cast(c)

c++ - 显式构造函数和 static_cast

structFoo{explicitFoo(inta):m(a){}intpadd1,m,padd2;};voidBar(Foo){}intmain(){Bar(11);//OK,giveserrorautox=static_cast(37);x.m;}static_cast构造Foo对象是否可以,即使它的构造函数被标记为explicit?它适用于MSVC2013和GCChttp://ideone.com/dMS5kB 最佳答案 是的,static_cast将使用explicit构造函数。5.2.9Staticcast[expr.s

c++ - static_cast 与直接调用转换运算符?

考虑下面的类,作为一个简单的例子:#include#includeusingnamespacestd;classpoint{public:int_x{0};int_y{0};point(){}point(intx,inty):_x{x},_y{y}{}operatorstring()const{return'['+to_string(_x)+','+to_string(_y)+']';}friendostream&operator(p);//Option1os应该直接调用转换运算符,还是只调用static_cast并让它完成工作?这两行几乎会做完全相同的事情(即调用转换运算符),据我所

C++ 删除 static_cast<void*> (指针)行为

假设代码执行以下操作:T*pointer=newT();deletestatic_cast(pointer);结果是什么?未定义,内存泄漏,内存被删除? 最佳答案 行为未定义。关于delete表达式,C++标准说:Inthefirstalternative(deleteobject),ifthestatictypeoftheoperandisdifferentfromitsdynamictype,thestatictypeshallbeabaseclassoftheoperand’sdynamictypeandthestaticty

c++ - 关于static_cast的问题

我写了一段代码,但我对它的输出感到困惑:#includeusingnamespacestd;classB{public:virtualvoidfoo(){cout(pb);pd1->foo();pd1->disp();}intmain(intargc,char*argv[]){B*pb=newB();func(pb);return0;}输出是:B::fooD::disp但是据我所知,pb指向类型B。而且里面没有名为disp()的函数?那么,为什么它可以访问D类中的disp()函数? 最佳答案 因为disp()不访问类的任何成员,原则

c++ - std::static_pointer_cast 是否有任何额外的运行时开销?

相对于static_cast,即。所以,如果我们有这两个类型转换Base*b(newDerived());Derived*d=static_cast(b);//(1)shared_ptrb(newDerived());shared_ptrd=static_pointer_cast(b);//(2)第(2)行会比第(1)行慢吗? 最佳答案 是的,它有更多的开销,因为它必须返回一个新的shared_ptr而不是一个新的原始指针。boost实现是:templateshared_ptrstatic_pointer_cast(shared_p

c++ - 是否可以在成员例程中使用 const_cast 来避免重复代码

在那种情况下可以使用const_cast还是有任何注意事项:classA{public:A():m_someData(5){}int&get(){returnm_someData;};constint&get()const{const_cast(this)->get();};private:intm_someData;};这样做的目的是让get例程可能更加复杂,并且应该避免代码重复。 最佳答案 没有。我不建议那样做。我建议您反向使用const_cast:int&get(){returnconst_cast(const_cast(*t

c++ - 如何将低级常量应用于模板变量。我正在尝试编写一个 const_cast 实现

如何让这个static_assert传递到我失败的代码中?我尝试了T周围const的所有排列,但我无法获得constint*。编译器总是将其解释为int*const。templateunionconst_cast_impl{usingCT=constT;static_assert(std::is_same::value,"CTisnotconstint*");Tdata;CTcdata;const_cast_impl(CTptr):cdata(ptr){}operatorT(){returndata;}};intmain(){inta=2;constint*ptr=&a;int*ptr