草庐IT

auto_cast

全部标签

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

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

【C++】基础知识讲解(引用、内联、auto,基于范围for循环)

 🌈个人主页:秦jh__https://blog.csdn.net/qinjh_?spm=1010.2135.3001.5343🔥 系列专栏:http://t.csdnimg.cn/eCa5z目录引用概念特性使用场景作参数作返回值传值、传引用效率比较引用和指针的区别内联函数概念查看方式特性 宏的优缺点 C++代替宏的技术前言    💬hello!各位铁子们大家好哇。       今日更新了引用、内联、auto,基于范围for循环的内容    🎉欢迎大家关注🔍点赞👍收藏⭐️留言📝引用概念引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同

c++ - 如何使用 std::auto_ptr 声明动态数组?

我正在尝试声明一个动态int数组,如下所示:intn;int*pInt=newint[n];我可以用std::auto_ptr做到这一点吗?我试过类似的方法:std::auto_ptrpInt(newint[n]);但是它不编译。我想知道我是否可以使用auto_ptr构造声明一个动态数组,以及如何声明。谢谢! 最佳答案 不,你不能,也不会:C++98在数组方面非常有限,auto_ptr是一个非常笨拙的野兽,它通常不会做你需要的事情。您可以:使用std::vector/std::deque,或std::array,或者使用C++11和

c++ - 是否有 auto_ptr 的替代品可以与 c++11 中的 boost ptr_map 一起使用

在c++11中,auto_ptr已弃用,取而代之的是更合理的unique_ptr。唉,如果你使用boost::ptr_map,auto_ptr就完成了一个非常方便的用途:std::auto_ptrpLayer(newLayer());mRawLayerPtrMap.insert(layerName,pLayer);是否有可能使用与c++11类似的东西。这个我知道Layer*pLayer=newLayer();mFusedLayers.insert(fusedLayerName,pLayer);有效,但auto_ptr在一些更复杂的场景中有它的优点。是否有适用于C++11的替代品?

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++ - foreach(int i.. 和 foreach(auto i

我正在MacOX(LLVM4.2)附带的Clang编译器上试验C++11功能,以下结果让我感到困惑://clangcompilewith"c++-std=c++11-stdlib=libc++"#include#includeintmain(void){usingnamespacestd;vectoralist={1,2,3,4};for(inti=0;i在运行环境中,我得到如下不同的输出:12342340为什么我会得到不同的结果? 最佳答案 for(autoi:alist)这会获取alist中的每个value,因此i变为:1,2,

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++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 'for(auto &str : vec)' 内部 for 循环的目的是什么?

我是C++的新手,正在尝试学习vector的概念。我在网上看到这段代码。我的问题是,'for(auto&str:vec)'中的内部for循环的目的是什么?为什么作者要对第一个引用(&str)创建第二个引用(&c)?intmain(){vectorvec;for(stringword;cin>>word;vec.push_back(word)){}for(auto&str:vec){for(auto&c:str){c=toupper(c);}}for(inti=0;i!=vec.size();++i){if(i!=0&&i%8==0)cout 最佳答案