草庐IT

overloading

全部标签

c++ - 重载和 this 指针

这个问题更像是理论问题。前言。访客模式:classVisitor{public:virtualvoidVisitElementA(constElementA&obj)=0;virtualvoidVisitElementB(constElementB&obj)=0;};classElement{public:virtualvoidAccept(Visitor&visitor)=0;};classElementA:publicElement{public:voidAccept(Visitor&visitor)override{visitor.VisitElementA(*this);}};

c++类型转换运算符重载和隐式转换

如果我正在重载类型转换运算符,当需要隐式转换但找不到时,我会遇到编译错误。考虑一个简单的示例,其中我有一个包含类型(在本例中为longlong)的包装类:classmy_wrapper{longlongstate;public:my_wrapper(longlong_in):state(_in){}my_wrapper&operator=(constlonglongrhs){state=rhs;return*this;}operatorlonglong(){returnstate;}};现在的问题是,如果我想将代码转换为其他内容(例如void*...假设我为此使用64位),则如果不指定

c++ - 防止模板化成员函数中某些参数的隐式转换

目前我有一个这样定义的成员函数:templateboolupdateParameter(conststd::string&name,constT&data);指针重载。templateboolupdateParameter(conststd::string&name,T*data);我希望能够像这样使用这个函数:inttest=20;updateParameter("name",0);updateParameter("Referencedparameter",&test);这样我就可以拥有一个参数对象,该对象要么拥有它所代表的数据,要么指向用户拥有的成员。现在我遇到的问题是当前设置MS

c++ - const 指针和 const 数组的输出

当我们有两个运算符用于输出对象和这些对象的数组,并尝试输出常量对象数组时,就会涉及到对象运算符。有没有办法强制数组的运算符与常量对象的c数组一起使用?示例代码:#include#includeusingstd::size_t;namespacenm{structC{inti;};templateusingc_c_array=C[N];inlinestd::ostream&operatorinlinestd::ostream&operatorconst&){returnlhs输出:1211此外,在我的例子中,2运算符使用1作为输出。 最佳答案

c++ - 这个函数调用在 C++ 中是如何模糊的?

考虑以下程序:(请在此处查看现场演示http://ideone.com/7VHdoU)#includevoidfun(int*)=delete;voidfun(double)=delete;voidfun(char)=delete;voidfun(unsigned)=delete;voidfun(float)=delete;voidfun(longint);intmain(){fun(3);}voidfun(longinta){std::cout编译器给出以下错误:error:callofoverloaded'fun(int)'isambiguousfun(3);^但我不明白为什么以及

c++ - 模板函数重载(泛型 vs 模板模板类型)选择正确的重载

我有一个模板函数,只要模板类型是数字,它就会对给定值执行某些操作:templatevoidfunction(scalar_tvalue){ifconstexpr(std::is_floating_point_v){std::cout){std::cout这个模板的重载需要一个包含数字的容器:templateclasscontainer_t>voidfunction(constcontainer_t&container){for(constauto&value:container){function(value);std::cout使用上面的模板,我调用以下代码:intmain(){fu

c++ - 如何编码和重载返回自身的函数? C++17

使用最新的msvc(截至25DEC17)。templateautoout_(constT&val_){//dosomethingwithval_//error:cannotdeduceautofromout_returnout_;}问题是如何编写代码,然后对上面这个小“东西”写几个重载?对,必须是msvc和C++17。没有GCC7.0.2也不编译它。clang还没试过。也许仿函数模式可能有所帮助?请指教... 最佳答案 我很惊讶您的lambda解决方案可以与g++一起使用。clang++投诉error:variable'out_'d

c++ - 模板重载导致链接器错误/奇怪的行为

使用以下最小示例,我在visualstudio15.8.7(具有标准设置的标准控制台应用程序(仅删除预编译header))中的本地系统上出现链接器错误:“错误LNK1179文件无效或损坏:COMDAT重复??$f@H@@YAXH@Z'"#includetemplatevoidf(T){printf("1");}//#1.Tcanbededucedtemplatevoidf(int){printf("2");}//#2.Tneedstobespecifiedexplicitlyintmain(){f(8);//a)calls#1f(8);//b)calls#2}注释掉调用a)或调用b)将

c++ - 跨命名空间重载函数是否一定很困难?

我一直在用这种结构写一些代码namespaceutil{voidread(int&);templatevoidread(T&);}voidfoo();usingnamespaceutil;namespace{//Avoidread(MyType&,int);voiddo_something(){MyTypet;inti;//usingutil::read;//Bread(i);//Cread(t,i);//D}}voidfoo(){do_something();}起初C行没有编译,除非我将它完全限定为util::read(i)或未注释的B行,但这使得D行失败。特化模板util::rea

c++ - 分数类增量运算符重载解释

自上学期以来,我对此进行了很多思考(老实说)。而且我仍然不完全确定这里发生了什么。谁能帮助和启发我?我同意前/后缀的区别。这就是分数递增的方式,这让我很困惑以前缀为例。那么如果我有一个2/4的分数会增加到3/4吗?因为当我查看numer+=denom时,它让我认为它会返回2+2+4,即8。//prefixincrementoperatorfraction&fraction::operator++(){numer+=denom;return*this;}//postfixincrementoperatorfractionfraction::operator++(int){//Notedu