草庐IT

delete-operator

全部标签

c++ - 反向迭代器错误 : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator'

代码A:vector::const_reverse_iteratorrcit;vector::const_reverse_iteratortit=v.rend();for(rcit=v.rbegin();rcit!=tit;++rcit)cout代码B:vector::const_reverse_iteratorrcit;for(rcit=v.rbegin();rcit!=v.rend();++rcit)coutCODEA工作正常但是为什么代码B通过错误:DEVC++\vector_test.cpp在'rcit!=std::vector::rend()与_Tp=int,_Alloc=s

c++ - operator() 模板特化

我正在尝试对模板运算符进行特化,模板如下所示:templateResultTypeoperator()(Iterator1a,Iterator2b,size_tsize,ResultTypeworst_dist=-1)const在我做了如下所示的特化之后:templatefloatoperator()(float*a,floatconst*b,unsignedlongsize,floatworst_dist=-1)const编译时出现错误:Cannotspecializeafunction'operator()'withinclassscope所有这些函数都在结构模板中我很乐意得到一些

c++ - 'delete pointer' 仅仅意味着 '*pointer = 0' 吗?

#includeintmain(){usingstd::cout;int*p=newint;*p=10;cout输出:100x237c01000x237c010这里删除p后,为什么指针p还保留着它的值呢?不删除释放指针p?“释放指针”到底是什么意思?“删除p”是否只是意味着“*p=0”?(从输出看来) 最佳答案 Hereafterdeletingp,whythepointerpretainsitsvalue?这就是语言的设计方式。如果你想让你持有的指针归零,你需要自己将它归零。指针p是另一block内存,与它指向的分配/对象分开。D

c++ - 释放 : A** mat = new A*[2]; 的内存

我定义了:A**mat=newA*[2];但是我怎样才能删除它呢?使用delete[]mat;或delete[]*mat;? 最佳答案 它是delete[]mat;仅当您不进行额外分配时。但是,如果您在数组数组中分配了数组,则还需要删除它们:A**mat=newA*[2];for(inti=0;i!=2;i++){mat[i]=newA[5*(i+3)];}...for(inti=0;i!=2;i++){delete[]mat[i];}delete[]mat; 关于c++-释放:A**m

c++ - C++14 是否要求删除表达式必须调用 `void operator::delete(void*, std::size_t)` 而不是 `void::operator delete(void*)` ?

根据thisvoidoperatordelete(void*);(1)voidoperatordelete[](void*);(2)voidoperatordelete(void*,conststd::nothrow_t&);(3)voidoperatordelete[](void*,conststd::nothrow_t&);(4)voidoperatordelete(void*,std::size_t)(5)voidoperatordelete[](void*,std::size_t)(6)voidoperatordelete(void*,std::size_t,conststd:

c++ - 为什么在使用复制分配运算符时需要删除资源?

例如我的一本书中的代码:classHasPtr{public:HasPtr(constHasPtr&h):ps(newstd::string(*h.ps)),i(h.i){}HasPtr(conststd::string&s=std::string()):ps(newstd::string(s)),i(0){}HasPtr&operator=(constHasPtr&);~HasPtr(){deleteps;}private:std::string*ps;inti;};HasPtr&HasPtr::operator=(constHasPtr&rhs){autonewp=newstrin

c++ - operator<< 重载如何工作?

给定一个类:structemployee{stringname;stringID;stringphone;stringdepartment;};下面的函数是如何工作的?ostream&operatorcout为给定的employeee生成格式化输出.示例输出:AlexJohnsonEmp#:5719Dept:RepairPhone:555-0174我无法理解ostream函数的工作原理。它如何获取参数“ostream&s”?它如何重载“ 最佳答案 这称为重载解析。你写了cout.编译器将其视为operator,其中cout是ostr

c++ - 正确重载 new/delete new[]/delete[]

这是我之前问题的后续,Initializingaclassusingmalloc接受的问题答案有效并在avr-gcc上给我新建/删除,这是问题所在,但我重载的新删除对常规gcc造成严重破坏,重载新删除的正确方法是什么我所有的类派生自一个通用的基类,所以理想情况下我只想为我的对象覆盖新的删除,这样它就不会与STLstdlib等混淆。 最佳答案 'new'和'delete'可以在公共(public)Object基类中重载。因此,这将仅适用于该层次结构。classObject{public:void*operatornew(size_ts

c++ - delete[] 字符数组

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:delete[]suppliedamodifiednew-edpointer.UndefinedBehaviour?假设我使用newchar[number]分配了一些字符。是否可以只删除几个结束字符(类似于delete[](charArray+4);,据说会取消分配除前四个字符之外的所有字符)?我读到一些实现的new[]存储对象数组之前分配的对象数,以便delete[]知道要取消分配多少对象,所以按照我的要求做可能不安全......谢谢。编辑:使用单独的delete语句手动删除不需要的结束字节是否是一种安全的

c++ - 使用 C++0x : call to deleted constructor of 时的 clang++ 错误消息

您好,我已经将我的Xcode升级到4.2版,并将clang++升级到以下版本:Appleclangversion3.0(tags/Apple/clang-211.10.1)(basedonLLVM3.0svn)Target:x86_64-apple-darwin11.2.0Threadmodel:posix当尝试使用clang-std=c++0x编译以下代码时#include#include#includeclassilpConstraintImpl{public:virtual~ilpConstraintImpl(){}};classilpConstraint{public:ilpC