草庐IT

OPERATOR

全部标签

c++ - C++ 中的 new 返回 (void *) 吗?

这是一个简单的问题:使用new运算符是否返回类型为(void*)的指针?引用Whatisthedifferencebetweennew/deleteandmalloc/free?答案-它说newreturnsafullytypedpointerwhilemallocvoid*但根据http://www.cplusplus.com/reference/new/operator%20new/throwing(1)void*operatornew(std::size_tsize)throw(std::bad_alloc);nothrow(2)void*operatornew(std::siz

c++ - 为什么这个程序调用operator()而不是构造函数呢?

这是一个编译时没有警告的程序,例如GNUC++:$g++-ot-Wall-pedantic-Wshadowt.cpp$./t.exeCallingbarney::barney()Callingfoo::operator()()Callingbarney::barney()但它在MSVC++上完全无法编译:$cl/EHsct.cppMicrosoft(R)32-bitC/C++OptimizingCompilerVersion15.00.30729.01for80x86Copyright(C)MicrosoftCorporation.Allrightsreserved.t.cppt.cp

c++ - 重载后缀和前缀运算符

请考虑以下代码#includeusingnamespacestd;classDigit{private:intm_digit;public:Digit(intndigit=0){m_digit=ndigit;}Digit&operator++();//prefixDigit&operator--();//prefixDigitoperator++(int);Digitoperator--(int);intget()const{returnm_digit;}};Digit&Digit::operator++(){++m_digit;return*this;}Digit&Digit::op

c++ - 重载 operator[] 从 1 开始和性能开销

我正在做一些C++计算力学(不用担心,这里不需要物理知识),有些事情真的让我很困扰。假设我想表示一个3D数学vector(与std::vector无关):classVector{public:Vector(doublex=0.,doubley=0.,doublez=0.){coordinates[0]=x;coordinates[1]=y;coordinates[2]=z;}private:doublecoordinates[3];};到目前为止一切顺利。现在我可以重载operator[]来提取坐标:double&Vector::operator[](inti){returncoord

c++ - 为什么对箭头 (->) 运算符的调用会失败?

考虑以下代码:#includeclassTest{public:Test():a{0}{}voidprint()const{std::cout(){a=5;}voidoperator++(){++a;}public:inta;};intmain(){Testa;a.print();//Incrementoperatora.operator++();//CORRECT++a;//CORRECTa.print();//Indirectionoperatora.operator->();//CORRECTa->;//INCORRECTa.print();}为什么对第二个->运算符的调用不正确

c++ - 为什么我必须调用 operator<< 作为 SFINAE 使用 void_t 的方法?

我正在尝试定义一个has_ostream_operatorSFINAE测试,用于检查我是否可以计算出给定的类型。我让它工作,但前提是在我对has_ostream_operator的定义中我调用operator作为一种方法而不是作为中缀运算符。换句话说,这是可行的:decltype(std::declval().operator()))>这不是:decltype(std::declval()())>下面的测试用例(也可以在http://coliru.stacked-crooked.com/a/d257d9d6e0f3f6d9看到)。请注意,我包含了void_t的定义,因为我只使用C++1

c++ - 为什么选择非常量版本而不是类的 const 版本?

测试代码如下:structA{operatorint();operatorint()const;};voidfoo(constint);现在,在调用时:foo(A());//callsA::operatorint()为什么会这样alwayschoosesthenon-constversion?即使使operatorconstint()const;对调用foo()也没有任何影响。除了标准引用,有人可以从逻辑上解释背后的原因吗? 最佳答案 A()为您提供一个非const限定的临时A对象。A()表达式是一个右值表达式,是的,但这不会使A对

c++ - `operator()...`在C++代码中是什么意思?

我试图理解std::visit的例子来自cppreference,在那里我看到了以下代码行:templatestructoverloaded:Ts...{usingTs::operator()...;};templateoverloaded(Ts...)->overloaded;我不明白。什么operator()...在代码中是什么意思? 最佳答案 我想用一些历史课来补充这里的好答案。这里有很多层,所以让我们一层一层地剥开它们。可变参数模板(C++11)参数包包装扩展using声明用于引入基类成员可变参数using声明(C++17)

c++ - 为什么不为给定的转换运算符调用构造函数?

structA{};structB{B(A*pA){}B&operator=(A*pA){return*this;}};templatestructWrap{T*x;operatorT*(){returnx;}};intmain(){Wrapa;BoB=a;//error:conversionfrom‘Wrap’tonon-scalartype‘B’requestedoB=a;//ok}当oB那么构造为什么B::B(A*)未为Wrap::operatorT()调用?[备注:B::operator=(A*)为Wrap::operatorT()调用在下一条声明中]

【flink番外篇】7、flink的State(Keyed State和operator state)介绍及示例 - 完整版

Flink系列文章一、Flink专栏Flink专栏系统介绍某一知识点,并辅以具体的示例进行说明。1、Flink部署系列本部分介绍Flink的部署、配置相关基础内容。2、Flink基础系列本部分介绍Flink的基础部分,比如术语、架构、编程模型、编程指南、基本的datastreamapi用法、四大基石等内容。3、FlikTableAPI和SQL基础系列本部分介绍FlinkTableApi和SQL的基本用法,比如TableAPI和SQL创建库、表用法、查询、窗口函数、catalog等等内容。4、FlikTableAPI和SQL提高与应用系列本部分是tableapi和sql的应用部分,和实际的生产应