草庐IT

assignment-operator

全部标签

c++ - 初始化列表作为 operator[] 的参数

这个问题与讨论的问题有关here.我尝试使用初始化列表来创建要传递给operator[]的参数。#include#includestructA{std::string&operator[](std::vectorvec){returnvec.front();}};intmain(){//okstd::vectorvec{"hello","world","test"};Aa;//error:couldnotconvert'{"hello","world","test"}'to'std::vector...'a[{"hello","world","test"}];}我的编译器(GCC4.6

c++ - std::vector::assign/std::vector::operator=(const&) 是否保证重用 `this` 中的缓冲区?

如果我将一个vector分配或复制到另一个vector(其容量与前者的大小相同或更大),我可以假设后者的缓冲区将被重用吗?下面的例子证明我可以,但是,标准保证吗?std::vector::assign和std::vector::operator=在这方面的行为有什么不同吗?#include#include#includeintmain(){std::vectora{1,2,3,4,5};std::vectorb{1,2,3,4};std::vectorc{1,2,3,4,5,6,7,8,9,10};std::coutLiveexample.更新:Thisanswer提到voidassi

c++ - is_assignable 和 std::unique_ptr

Here是来自gcc的测试文件,livedemostructdo_nothing{templatevoidoperator()(T*){}};intmain(){inti=0;std::unique_ptrp1(&i);std::unique_ptrp2;static_assert(!std::is_assignable::value,"");//note!here.}std::is_assignableIftheexpressionstd::declval()=std::declval()iswell-formedinunevaluatedcontext,providesthemem

c++ - QMap/QHash operator[] 返回引用有效性

我想知道对Qt容器内的值的引用,尤其是QHash或QMap的有效期有多长。我所说的有效是指在插入或删除其他元素后是否保证仍指向映射/哈希内的正确位置。让我们看下面的代码:QHashdict;//orQMapdict;dict.insert('a',1);int&val(dict['a']);dict.insert('b',2);val=3;//在最后一行设置值是否会正确地将与a关联的值更新为3或者它会导致段错误还是未定义(所以有时工作,其他时候出现段错误,这取决于是否必须在内部重组数据结构,例如调整哈希表数组的大小)。QMap和QHash的行为是否相同,或者一个有效而另一个无效?

c++ - 避免在复制构造函数和 operator= 中重复相同的代码

在C++中,当类包含动态分配的数据时,显式定义复制构造函数、operator=和析构函数通常是合理的。但是这些特殊方法的事件是重叠的。更具体地说,operator=通常首先进行一些破坏,然后进行类似于复制构造函数中的处理。我的问题是如何在不重复相同的代码行并且不需要处理器做不必要的工作(比如不必要的复制)的情况下以最好的方式编写它。我通常有两种帮助方法。一种用于build,一种用于破坏。第一个是从复制构造函数和operator=中调用的。第二个由析构函数和operator=使用。示例代码如下:templateclassMyClass{private://Datamembersintco

c++ - C2280 : attempting to reference a deleted function (union, 结构体,复制构造函数)

当我尝试在VisualStudio2015中编译以下最小示例时,我遇到了误导性错误消息的问题:classVector{floatx;floaty;public:Vector(floatx,floaty):x(x),y(y){}Vector&operator=(constVector&v){x=v.x;y=v.y;return*this;}//Vector(Vector&&)=default;};classRect{public:union{struct{Vectorp1,p2;};struct{floatp1x,p1y,p2x,p2y;};};Rect():p1(0,0),p2(0,0

c++ - 重载抽象运算符时出现 Clang 链接器错误=

VisualStudio2013编译器可以很好地处理以下代码,但clang5.0和6.2给我一个链接器错误:#includeusingnamespace::std;classIBase{public:virtualIBase&operator=(constIBase&other)=0;};classBase:virtualpublicIBase{public:Base&operator=(constIBase&other)override{constBase&b=dynamic_cast(other);return*this=b;}virtualBase&operator=(const

c++ - 不要临时调用 QString::operator[]()

我运行clazy在我的代码上并获得有关此类代码的警告:QCharvalue()const{if(hide_content_)return'\0';elsereturntext()[0];}text()有这样的签名QStringtext()const;警告是:warning:Don'tcallQString::operator[]()ontemporary[-Wclazy-detaching-temporary]returntext()[0];^但这是什么意思呢?临时QString对象是否有可能被销毁在调用operator[]之前? 最佳答案

c++ - 带有 nothrow 选项的 Operator new 仍然抛出异常

有这样的代码:#includeintmain(){for(;;){int*ptr=new(std::nothrow)int;if(ptr==0){std::cout然而,这个程序仍然抛出std::bac_alloc异常,尽管new是用std::nothrow参数调用的。该程序在VisualC++2010中编译,为什么会抛出异常?编辑:在Windows上从mingw使用g++,一切正常。 最佳答案 0必须格式化为"0"。这将占用几个字节;我敢打赌这就是原因。在std::bad_alloc::bad_alloc上放置一个断点,你就会知道

c++ - 在 C++ 中,在 STL 容器中存储具有重载 "operator&"的类对象是否合法?

根据C++03标准(23.1/3),只有copy-constructible类对象可以存储在STL容器中。Copy-constructible在20.1.3中描述并且要求“&”产生对象的地址。现在假设我有这个类:classClass{public:Class*operator&(){//dosomeloggingreturnthis;}constClass*operator&()const{//dosomeloggingreturnthis;}//whateverelse-assumeitdoesn'tviolaterequierements};此类对象是否可以合法存储在STL容器中?