草庐IT

OPERATOR

全部标签

c++ - 在一次调用中根据 operator< 实现其他比较运算符

我已经看到了,如果你有operator,你可以实现,比如说,operator>=作为!(a.然而,当我去布里斯托尔的C++委员会时,他们说你可以用这种方式实现所有比较运算符。特别是,当考虑具有非平凡比较运算符的类型时(考虑任意长的字符串,特别是如果这是Unicode),只需调用一次operator即可完成。.我无法想象如何做到这一点。我如何实现operator>和operator只需调用一个电话operator而对其他比较运算符没有影响? 最佳答案 a>b==baa>=b==!(a甚至可以根据小于来实现相等性(有点滥用我的元语法):

c++ - 我相信这是 clang++ 中与访问类的公共(public)成员函数相关的错误

以下doesn'tcompile在clang中:#includevoidf(){std::cout产量:main.cpp:13:16:error:unknowntypename'p';didyoumean'S::p'?s.operatorp()();^S::pmain.cpp:6:19:note:'S::p'declaredheretypedefvoid(*p)();^但它应该,因为表达式s.operatorp()()访问对象S::s的公共(public)成员函数。我错过了什么吗?如果我错了,我将不胜感激标准中的引述来支持答案。 最佳答案

c++ - 类型转换指针和三元? : operator. 我是重新发明了轮子吗?

此代码的最后一行无法使用castingAndTernary.cpp:15进行编译:错误:不同指针类型“D1*”和“D2*”之间的条件表达式缺少强制转换一个真正聪明的编译器可能没有任何困难,因为两者都可以安全地转换为B*(基类)。我不愿意使用static_cast和dynamic_cast等等——我担心有一天我会混淆这些类并得到未定义的行为。这就是我创建up_cast模板的原因。该模板在允许的转换方面做了最低限度的工作。有更简单的方法吗?还有其他解决方法,但我不禁想到我可以使用更简单、更安全的方法吗?structB{};structD1:publicB{};structD2:public

c++运算符重载的多态性

如何使纯虚函数成为operator+();功能。我在基础课上喜欢这个吗int运算符+()=0;编译器给出错误。在派生类operator+()函数中编译器说派生类不能使.因为下面的类是抽象的我知道我不能创建抽象类的对象,但现在我尝试创建派生类对象。这是代码#includeusingnamespacestd;classana{protected:intx;public:virtualintoperator+()=0;virtualvoidoperator=(ana&)=0;};classbaba:publicana{public:baba(intk){x=k;}intoperator+(b

c++ - 不能将 operator= 与 std::stringstream 一起使用

我正在尝试制作struct,其中一名成员属于std::stringstream类型。我正在使用C++11,并根据http://www.cplusplus.com/reference/sstream/stringstream/operator=/我能行。这是我的代码:structlogline_t{stringstreamlogString;/*!它没有编译,因为我收到这个错误:error:useofdeletedfunction‘std::basic_stringstream&std::basic_stringstream::operator=(conststd::basic_stri

c++ -::operator new(size_t) 是否使用 malloc()?

::operatornew(size_t)是在内部调用malloc(),还是直接使用系统调用/操作系统特定的库调用?C++标准怎么说?在thisanswer它说:malloc()isguaranteedtoreturnanaddressalignedforanystandardtype.::operatornew(n)isonlyguaranteedtoreturnanaddressalignedforanystandardtypenolargerthann,andifTisn'tacharactertypethennewT[n]isonlyrequiredtoreturnanaddr

c++ - 为什么 std::chrono::duration::operator*= 不像内置的 *=?

如std::chrono::duration::operator+=中所述签名是duration&operator*=(constrep&rhs);这让我很奇怪。我假设持续时间文字可以像任何其他内置一样使用,但事实并非如此。#include#includeintmain(){usingnamespacestd::chrono_literals;autom=10min;m*=1.5f;std::cout输出是150%of10min:10min150%of10:15为什么这样选择界面?在我看来,这样的界面templateduration&operator*=(constT&rhs);会产生

c++ - 插入运算符没有产生预期的输出?

这是代码#includeusingnamespacestd;main(){cout我知道cout被解释为cout.operator但是这段代码产生了以下结果HellowWorld0x8048830如果我使用operator工作正常cout.operator之间有什么区别?和operator 最佳答案 std::basic_ostream重载operator分为两组,members和non-members.不同之处在于当您编写cout时constchar*的非成员重载选择打印字符串文字。调用成员(member)版时,最合适的只有voi

c++ - 使用继承时实现 operator==

我有一个实现==运算符的基类。我想写另一个类,继承基类,并且应该重新实现==运算符。这是一些示例代码:#include#includeclassPerson{public:Person(std::stringName){m_Name=Name;};booloperator==(constPerson&rPerson){returnm_Name==rPerson.m_Name;}private:std::stringm_Name;};classEmployee:publicPerson{public:Employee(std::stringName,intId):Person(Name)

c++ - 比较仿函数类型与运算符<

在GoogleC++StyleGuide,sectiononOperatorOverloading建议不要重载任何运算符(“除非在罕见的特殊情况下”)。具体来说,它建议:Inparticular,donotoverloadoperator==oroperatorjustsothatyourclasscanbeusedasakeyinanSTLcontainer;instead,youshouldcreateequalityandcomparisonfunctortypeswhendeclaringthecontainer.我不太清楚这样的仿函数是什么样的,但我的主要问题是,为什么您要为