草庐IT

copy-initialization

全部标签

c++ - GNU STL 字符串 : is copy-on-write involved here?

(免责声明:我不知道C++标准对此会说什么……我知道,我很糟糕)在处理非常大的字符串时,我注意到std::string正在使用写时复制。我设法编写了最小的循环来重现观察到的行为,例如,下面的循环运行得非常快:#includeusingstd::string;intmain(void){stringbasestr(1024*1024*10,'A');for(inti=0;i在循环体a_copy[1]='B';中添加写入时,显然发生了实际复制,并且程序在0.3秒内运行,而不是几毫秒。100次写入使其速度减慢了大约100倍。但后来变得很奇怪。我的一些字符串没有写入,只是读取,这没有反射(re

c++ - 复制构造函数 : deep copying an abstract class

假设我有以下情况(简化情况):classColor;classIColor{public:virtualColorgetValue(constfloatu,constfloatv)const=0;};classColor:publicIColor{public:floatr,g,b;Color(floatar,floatag,floatab):r(ar),g(ag),b(ab){}ColorgetValue(constfloatu,constfloatv)const{returnColor(r,g,b)}}classMaterial{private:IColor*_color;publ

c++ - 为什么将这些 C++ header 指定为包含 <initializer_list>?

从https://stackoverflow.com/a/26614984/481267可以看出以下header由标准保证#include:[容器]中的所有内容这些头文件中的大多数都声明了至少一个函数,该函数采用std::initializer_list论据,所以说得通。然而,,,和没有这样的功能,尽管在这里统一处理所有容器可能是有意义的。没有这样的功能。确实具有initializer_list的功能参数(rbegin,rend)但未指定包含.这些异常(exception)的原因是什么? 最佳答案 似乎没有明确的理由,只是提出了一些

c++ - 在 QObject 派生类中重复 Q_DISABLE_COPY

在Qt中有一个宏允许为类声明私有(private)复制构造和赋值运算符:http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#Q_DISABLE_COPY据说这个宏应该用于所有QObject(尤其是QWidget)的派生类。我了解它的工作原理以及它为何有用。我不明白的是:有什么理由在我的QObject派生类中重复Q_DISABLE_COPY而QObject已经包含Q_DISABLE_COPY并且通过这有效地防止我的派生类被复制? 最佳答案 尝试复制派生类时可能打印的错误消息可能是指

c++ - 错误 : ISO C++ forbids in-class initialization of non-const static member

这是头文件:employee.h#ifndefEMPLOYEE_H#defineEMPLOYEE_H#include#includeusingnamespacestd;classEmployee{public:Employee(conststring&first,conststring&last)重载的构造函数:firstName(first),firstName重载构造函数lastName(last)lastName重载构造函数{//Theconstructorstart++counter;它为每个创建的对象加一;cout析构函数cout返回每个对象的名字和姓氏--counter;计

c++ - std::make_shared 与 std::initializer_list

#include#includeclassBase{public:Base(){}};classDerived:publicBase{public:Derived(){}Derived(std::initializer_list>>){}};intmain(intargc,char**argv){autoexample=newDerived({{0,std::make_shared()}});return0;}它正常工作(livepreview),但是当我尝试使用std::make_shared和std::initializer_list作为参数时,我得到了错误:autoexample

C++20 constexpr std::copy 运行时优化

cppreference.com说:Inpractice,implementationsofstd::copyavoidmultipleassignmentsandusebulkcopyfunctionssuchasstd::memmoveifthevaluetypeisTriviallyCopyable但是,该页面还指出,从C++20开始,不采用执行策略的重载将是constexpr。标准会禁止这些运行时优化(因为std::memmove不是constexpr)还是有办法为运行时优化constexpr函数? 最佳答案 我们也可以吃蛋

C++ 错误 - "member initializer expression list treated as compound expression"

我遇到了一个我不熟悉的C++编译器错误。可能是一个非常愚蠢的错误,但我不能完全指出它。错误:test.cpp:27:error:memberinitializerexpressionlisttreatedascompoundexpressiontest.cpp:27:warning:left-handoperandofcommahasnoeffecttest.cpp:27:error:invalidinitializationofreferenceoftype‘constBar&’fromexpressionoftype‘int’代码:1#include23classFoo{4publ

C++ : Implementing copy constructor and copy assignment operator

在阅读了C++中的复制构造函数和复制赋值运算符之后,我尝试创建一个简单的示例。虽然下面的代码片段显然有效,但我不确定我是否以正确的方式实现了复制构造函数和复制赋值运算符。您能否指出是否有任何错误/改进或更好的示例来理解相关概念。classFoobase{intbInt;public:Foobase(){}Foobase(intb){bInt=b;}intGetValue(){returnbInt;}intSetValue(constint&val){bInt=val;}};classFoobar{intvar;Foobase*base;public:Foobar(){}Foobar(i

C++ "No matching constructor for initialization of"编译器错误

我有一个类,我尝试初始化但收到错误“没有匹配的构造函数用于初始化'TextureCoordinates'”;我正在尝试初始化的类:classTextureCoordinates{public:TextureCoordinates(){};TextureCoordinates(Point2D&origin,Dimensions2D&dim);Point2DgetOrigin()const{returnorigin;};Dimensions2DgetDim()const{returndim;};private:Point2Dorigin;Dimensions2Ddim;};编译器错误:Te