草庐IT

conversion-operator

全部标签

c++ - 在 C : Derived to base conversions 中包装 C++

我正在将一个简单的C++继承层次结构包装到“面向对象的”C中。我试图弄清楚在将指向C++对象的指针视为指向不透明C结构的指针时是否存在任何问题。特别是在什么情况下派生到基的转换会出现问题?类本身比较复杂,但层级较浅,仅采用单继承://AbaseclasswithlotsofimportantsharedfunctionalityclassBase{public:virtualvoidsomeOperation();//Moreoperations...private://Data...};//OneofseveralderivedclassesclassFirstDerived:pub

C++ : friend function in a template class for operator<<

在.cpp文件中声明模板类的友元函数(对于std::ostream&运算符?我当前的实现不起作用://MyTest.htemplateclassMyTest{inlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs);};//MyTest.cpptemplateinlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs){//IMPLEMENTATION}非常感谢! 最佳答案 引用op

c++ - operator()() 定义了什么?

如果这个问题被举报,我很抱歉,但我似乎无法轻松地在线找到解决方案。如果我覆盖operator()()这定义了什么行为? 最佳答案 operator()是函数调用操作符,即可以使用对应类型的对象作为函数对象。第二组括号包含空的参数列表(和往常一样)。例如:structfoo{intoperator()(){return17;};};intmain(){foof;returnf();//useobjectlikeafunction}上面的例子只是展示了运算符是如何声明和调用的。实际使用可能会访问运算符中的成员变量。函数对象在标准C++库

c++ - 为什么 std::vector 需要 operator =

我有一个关于我们可以存储在vector中的类的问题。可以存储在vector中的要求是什么?似乎这样的类必须有赋值运算符。但我不确定是否仅此而已。让我举个例子。A类有constint成员。如果我不写operator=,它就不会编译。但是在这个例子中,这个操作符什么都不做。该程序正确显示10和20。看起来operator=是必需的,但实际上并没有使用。#include#includeclassA{public:A(inta):a_(a){}A&operator=(constA&a2){return*this;}//Withoutthis,compilefails.voidprint()co

c++ - 传入 A::operator new() 的大小是否总是等于 sizeof(A)?

structAfinal{inta;void*operatornew(size_tsize){////Issizealwaysequaltosizeof(A)here?//return::operatornew(size);}voidoperatordelete(void*ptr){::operatordelete(ptr);}};intmain(){for(autoi=0;i我的问题也嵌入了代码中。C++标准是否保证传入A::operatornew()的大小始终相同?更新:在这里,只考虑A是最终类。 最佳答案 引自C++11标准,

c++ - 警告 C4244 : 'argument' : conversion from 'double' to 'const int' , 可能丢失数据

我正在定义“*”运算符以使用“NumericArray”类模板。代码如下:templateNumericArrayNumericArray::operator*(constT&factor)const{NumericArraynewArray(Size());for(inti=0;i当我尝试将类型为“int”的“NumericArray”(NumericArray)与“*”运算符一起使用时,当“factor”参数为double时:intArray1=intArray1*2.5;我收到以下编译器警告:warningC4244:'argument':conversionfrom'doubl

c++ - 在 C++ 的三规则中,为什么 operator= 不调用复制构造函数?

以下“最小”示例应显示ruleof3(andahalf)的用法.#include#includeclassC{std::string*str;public:C():str(newstd::string("defaultconstructed")){std::cout像这样编译它(g++版本:4.7.1):g++-Walltest.cpp-otest现在,应该发生什么?我假设行a=b.get_new();会制作一个硬拷贝,即分配一个新字符串。原因:operator=()按照此设计模式中的典型情况采用其参数,每个值调用一个复制构造函数,该构造函数将进行深层复制。究竟发生了什么?stdcto

c++ - 错误 : invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'

我想获取z_Data的第48个字符的第6位{charc=pPkt->z_Data[47];//thisz_Dataisacharbufferstd::cout>3)&1>4)&1>5)&1 最佳答案 优先级高于&,所以你需要:std::cout>3)&1)>4)&1)>5)&1) 关于c++-错误:invalidoperandsoftypes'int'and''tobinary'operator https://stackoverflow.com/questions/246

c++ - 为什么fstream不利用operator>>的istream原型(prototype)?

我有一个使用友元函数重载运算符的类>>。重载运算符方法在标准cin使用上测试得很好。但是,当我尝试将代码升级为使用ifstream对象而不是istream对象时,原型(prototype)未被识别为有效方法。我的理解是ifstream继承自istream,因此,多态性应该允许ifstream对象与istream重载函数一起操作。我的理解有什么问题吗?是否有必要为每种输入流类型复制函数?类:#include#include#includeusingnamespacestd;classHospital{public:Hospital(std::stringname);std::string

c++ - 为什么 vector::operator[] 的实现方式与 map::operator[] 不同?

std::vector的operator[]是否有任何理由只返回一个引用而不是插入一个新元素?vector::operator的cppreference.com页面显示hereUnlikestd::map::operator[],thisoperatorneverinsertsanewelementintothecontainer.map::operator[]的页面says"Returnsareferencetothevaluethatismappedtoakeyequivalenttokey,performinganinsertionifsuchkeydoesnotalreadye