草庐IT

auto-delete

全部标签

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 最佳答案

c++ - 为什么这个无锁堆栈类中的 'deleting' 节点会导致竞争条件?

在AnthonyWilliams的《C++ConcurrencyinAction》一书中,第7.2.1节列出了一个无锁堆栈实现:templateclasslock_free_stack{structnode{shared_ptrdata_;node*next_;node(constT&data):data_(make_shared(data)){}};atomichead_;public:voidpush(constT&data){node*new_node=newnode(data);new_node->next_=head_.load();while(!head.compare_e

c++ - 带有 if 语句的 auto 函数不会返回值

我制作了一个模板和一个auto函数,用于比较2个值并返回最小值。这是我的代码:#includeusingnamespacestd;//Templatewithavaluereturningfunction:PrintSmallertemplateautoPrintSmaller(TNumOne,UNumTwo){if(NumOne>NumTwo){returnNumTwo;}else{returnNumOne;}}intmain(){intiA=345;floatfB=23.4243;cout但它无法编译,我在VS2015上遇到此错误:错误C3487“int”:所有返回表达式必须推导出

c++ - 循环依赖 : can't delete an incomplete type

我不明白为什么会出现此编译器错误:errorC2027:useofundefinedtype'GameState'note:seedeclarationof'GameState'errorC2338:can'tdeleteanincompletetypewarningC4150:deletionofpointertoincompletetype'GameState';nodestructorcalled这是相关代码:#pragmaonce#include#include"SpawnManager.h"#include"Resource.h"#include#includeclassGa

c++ - 是否需要将 delete 用于在 vector 中创建的新数组?

我正在尝试为void**数据数组创建一些动态数组。std::vectordata;data.push_back(newdouble[1024]);//arrayofdoublesdata.push_back(newshort[1024]);//arrayofshorts为了清理我应该只使用data.clear();或者是否需要删除每个新的(s),如果需要,如何完成? 最佳答案 ForcleanupshouldIjustusedata.clear();这将从vector中删除所有指针。如果其中任何一个是指向它们各自动态对象的唯一拷贝,

c++ - 我可以使用 auto 或 decltype 代替尾随返回类型吗?

我发现尾随返回类型很容易定义返回复杂类型的函数的返回值,例如:autoget_diag(int(&ar)[3][3])->int(&)[3]{//usingtrailingreturntypestaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;}auto&get_diag2(int(&ar)[3][3]){//adding&autobecauseotherwiseitconvertsthearraytopointerstaticintdiag[3]{ar[0][0],ar[1][1],ar[2][2]};returndiag;

C++——这里是否存在从 Fred* 到 auto_ptr<Fred> 的隐式转换?

我看到了下面的代码,#include#includeusingnamespacestd;classFred;//Forwarddeclarationtypedefauto_ptrFredPtr;classFred{public:staticFredPtrcreate(inti){returnnewFred(i);//Isthereanimplicitcastinghere?Ifnot,howcanwereturn//aFred*withreturnvalueasFredPtr?}private:Fred(inti=10):i_(i){}Fred(constFred&x):i_(x.i_

c++ - 如果我有运算符 T *(),是否需要重载 delete?

如果我有一个包含指针的模板类A,并且A有一个将返回该指针的隐式转换运算符,我是否需要,或者我应该,为A定义一个delete运算符,如果我打算将delete应用于此类的对象? 最佳答案 如果定义operatornew,则只需定义operatordelete——在这种情况下,您几乎必须这样做。这并不意味着某些东西不需要删除您的A*——但您不需要为此定义任何运算符,它会默认工作。 关于c++-如果我有运算符T*(),是否需要重载delete?,我们在StackOverflow上找到一个类似的问

c++ - 我应该明确地零初始化 auto_ptr 吗?

我的一些同事更喜欢在构造函数初始化列表中将std::auto_ptr显式初始化为0,但它会被初始化为0在它的构造函数中没有任何显式初始化。那么有什么理由这样做吗?#includeclassA{A():SomePtr(0){}private:std::auto_ptrSomePtr;}; 最佳答案 不,std::auto_ptr的默认构造函数正是这样做的,因此没有必要显式地这样做。无论如何,这是风格问题,您应该保持一致。例如,您是否会在构造函数初始化列表中显式调用成员vector的默认构造函数?作为旁注,std::auto_ptr在即

c++ - 我可以创建一个 auto_ptr 数组吗?

我有一个基类,它被多个派生类继承。我想创建baseClass指针的自动指针数组。当我初始化那些自动指针时,我遇到了一些编译时错误,然后我尝试这样做std::auto_ptrpbase[3];std::auto_ptrb1(newderived1());std::auto_ptrb2(newderived2());std::suto_ptrb3(newderived3());pbase[0]=b1;pbase[1]=b2;pbase[2]=b3;它工作正常,我修复了内存泄漏问题,而我是一个窗口,我不使用valgrind,我使用boost框架来解决泄漏问题。对于编译错误:classA{pu