草庐IT

delete-operator

全部标签

C++ Armadillo : GCC vs VC++2013: Operator () and overloading

我正在尝试使用ArmadilloC++库开发Linux/Win64应用程序。以下代码在GCC-4.7中编译,但在使用Armadillo提供的VS项目文件的VisualStudio2013中编译失败。#include#include"armadillo"usingnamespacearma;usingnamespacestd;//worksinGCC-4.7//VC++2013:compileerror:C3066voidfoo1(vec::fixed&bar){bar(1)=1.;}//worksvoidfoo2(vec::fixed&bar){bar.at(2)=1.;}//work

C++11 operator""with double parameter

考虑:structstr{};stroperator""_X(longdoubled){returnstr();}这在g++4.7.2Wallstd=c++11下编译得很好但现在如果我给双倍:stroperator""_X(doubled){returnstr();}我收到以下错误消息:main.cpp|3|错误:'stroperator""_X(double)'的参数列表无效问题是什么?这与“无法重新定义内置文字后缀的含义”(StroustrupFAQ)有关吗?您能想出解决方法吗? 最佳答案 Whatistheproblem?问题

c++ - 为什么 std::shared_ptr 从基类和派生类调用析构函数,而 delete 只调用基类的析构函数?

这个问题在这里已经有了答案:Howisitpossible(ifitis)toimplementshared_ptrwithoutrequiringpolymorphicclassestohavevirtualdestructor?(3个答案)关闭8年前。为什么在使用std::shared_ptr释放时从基类和派生类调用析构函数,而第二个示例仅从基类调用析构函数?classBase{public:~Base(){std::coutsharedA(newDerived);}std::cout输出:--------------------DeriveddestructorBasedestr

C++ 为中间件生成的类型提供 `operator<<` 的通用方法

我们使用中间件为我们生成各种编程语言(包括C++)的类型。对于为C++生成的结构,我想注入(inject)可用于各种数据转换的代码,例如输出到std::ostream.假设我们生成了以下结构:structFoo{inta;doubled;};假设我更改了中间件编译器以生成以下模板函数:templatevoidvisit(Visitor&v,constFoo&data){v.visit("a",data.a);v.visit("d",data.d);}现在我可以以各种方式使用这段代码,如果不使用它应该不会影响任何东西,例如makestd::ostream::operator:struct

c++ - delete[] (ptr, 0) 的行为

是否定义了这段代码的行为?int*ptr=newint[10];operatordelete[](ptr,0);这段代码编译得很好,(在我的机器上)似乎什么也没发生。它的行为是否在某处定义? 最佳答案 在这个声明中operatordelete[](ptr,0);显式调用了释放函数voidoperatordelete[](void*,std::size_t)noexcept;调用中类型为size_t的第二个参数刚设置为0。当分配的内存大小不等于0时,第二个参数等于0的调用行为是未定义的。

c++ - 重载 'operator++' 必须是一元或二元运算符(有 3 个参数)

我有一个头文件和一个.cpp文件。我正在尝试实现前缀和后缀运算符重载,但在设置重载时我一直收到此错误。分数.h#ifndefFRACTION_H#defineFRACTION_H#includeusingnamespacestd;classFraction{public:Fraction();Fraction(int,int);intgetTop(){returnm_top;}intgetBottom(){returnm_bottom;}voidset(intt,intb){m_top=t;m_bottom=b;reduce();}protected:private:voidreduc

c++ - 在什么情况下 std::unique_ptr::operator[] 可能抛出?

我的类有一个operator[],它所做的就是在unique_ptr上调用std::unique_ptr::operator[]>成员(member)。相关部分就是这样:templatestructFoo{T&operator[](constsize_tpos)constnoexcept{returndata_[pos];}std::unique_ptrdata_;};我已将运算符标记为noexcept。但是,unique_ptr::operator[]不是noexcept。我无法找出原因,也不知道我是否可以假设它永远不会抛出。unique_ptr::operator[]本身没有在文档

c++ - 在派生类中重写运算符 new/delete

我有一个无状态的抽象基类,各种具体类都从中继承。其中一些派生类也是无状态的。因为它们中的许多是在运行期间创建的,所以我想通过覆盖运算符new()/delete()让所有无状态派生类模拟单例来节省内存和开销。一个简化的例子看起来像这样:#includestructBase{virtual~Base(){}protected:Base(){}//preventconcreteBaseobjects};structD1:publicBase{//statefulobject--defaultbehaviorintdummy;};structD2:publicBase{//statelesso

c++ - 显式删除析构函数而不调用 delete

我正在阅读C++11FAQ并注意到这一点:classX4{~X4()=delete;//Disallowdestruction}ThisimplicitlyalsodisallowsmovingofX4s.Copyingisallowed,butdeprecated.我还找到了thisquote.Deletingthedefinitionofadestructorwillrequireallocationonthefree-storebecausestaticandautomaticobjectsimplicitlyinvokethedestructor:`structC{~C()=d

c++ - 如何在 C++ 中为多态类实现 operator==

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:What’stherightwaytooverloadoperator==foraclasshierarchy?我有一个基类和几个派生类,如下面的代码所示:classBase{public:friendbooloperator==(constBase&,constBase&);virtual~Base(){}private:virtualboolequals(constBase&other)const=0;};booloperator==(constBase&lhs,constBase&rhs){return