草庐IT

compound-operator

全部标签

c++ - 防止继承 operator new 和 delete

(我确实扫描了,但找不到任何类似的东西,如果有欺骗请关闭)。有没有办法防止这两个运算符被继承?例如:structfoo{staticvoid*operatornew(std::size_t){//special}staticvoidoperatordelete(void*p,std::size_t){//special}};structbar:publicfoo{};现在bar将继承这两个运算符-在这种微不足道的情况下,没什么大不了的,如果foo和bar中有数据成员,就会出现问题(在我的情况下更糟,因为foo的分配需要与bar不同!)现在避免这种情况的方法是在bar,我也会实现操作符。

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++ - 在 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

c++ - 了解 cout.operator<<() 的行为

根据thisquestion的最佳答案,cout相当于cout.operator.根据对thisquestion的回复,以上说法不正确。根据我自己的测试,cout.operator与cout相同当给定一个整数时。当给出一个float时,cout.operator将其强制为整数。当给定字符串文字时,如cout.operator,它输出的似乎是一个内存地址。当给定一个包含std::string的变量时,它会给出编译器错误。谁能给出初级到中级水平的解释是怎么回事? 最佳答案 这取决于expr.这两个问题的答案都是针对特定案例的,而不是一揽