草庐IT

copy_pointer

全部标签

c++ - 为什么将数组的地址分配给指针 "my_pointer = &my_array"是编译错误?

intmy_array[5]={0};int*my_pointer=0;my_pointer=&my_array;//compilererrormy_pointer=my_array;//ok如果my_array是数组的地址,那么&my_array会给我什么?我收到以下编译器错误:error:cannotconvert'int(*)[5]'to'int*'inassignment 最佳答案 my_array是一个由5个整数组成的数组的名称。编译器会很乐意将其转换为指向单个整数的指针。&my_array是一个指向5个整数数组的指针。编

C++11:将 std::is_pointer 扩展为 std::shared_ptr

我想重载std::is_pointer在C++11中为std::shared_ptr生成真值同样,因为后者的行为非常类似于T*.#includenamespacestd{templatestructis_pointer>:std::true_type{};templatestructis_pointer>:std::true_type{};}我想知道为什么这个重载还没有包含在标准实现中。有没有我忽略的陷阱?当然也可以引入一个新特性is_shared_ptr.其实我一开始就尝试了下面的代码:templatestructis_pointer::type>>:std::true_type{}

c++ - static_cast<Derived *>(Base pointer) 是否应该给出编译时错误?

static_cast(Basepointer)是否应该给出编译时错误?classA{public:A(){}};classB:publicA{public:B(){}};intmain(){A*a=newA();B*b=static_cast(a);//CompileError?} 最佳答案 它不会给出编译时错误,因为Base-Derived关系可以在运行时存在,具体取决于被强制转换的指针的地址。static_cast总是成功,但如果你不转换为正确的类型,则会引发undefined-behavior。dynamic_cast可能会

c++ - 更漂亮的 "pointer to last element"语法,std::vector?

我想知道是否有更漂亮的语法来获取指向C++vector中最后一个元素的普通指针(不是迭代器)std::vectorvec;int*ptrToLastOne=&(*(vec.end()-1));//theotherwayIcouldseewasint*ptrToLastOne2=&vec[vec.size()-1];但是这两个都不是很好看! 最佳答案 int*ptrToLastOne=&vec.back();//precondition:!vec.empty() 关于c++-更漂亮的"po

c++ - "smart pointer to member"的真实示例是什么?

为了澄清英语中可能存在的优先级歧义:我们正在讨论“智能(指向成员的指针)”,而不是“指向成员的(智能指针)”。我会将指向成员的智能指针定义为带有operator->*(T*lhs,Xrhs)的类X。在他的文章"Implementingoperator->*forSmartPointers",ScottMeyers只是简单地触及smart指向成员的指针,因为当时(1999年)具体问题对于原始指向成员的指针(旁注:后者可以用lambdahere优雅地解决)。无论如何,ScottMeyers在脚注中写道:Shortlyafterwritingthedraftofthisarticle,one

c++ - C++ 中 std::is_trivially_copy_constructible 中的琐碎操作是什么

这是std::is_copy_constructible(1)和std::is_trivially_copy_constructible文档的摘录(2)关于cppreference.com:1)CheckswhetheratypeisCopyConstructible,i.e.hasanaccessibleexplicitorimplicitcopyconstructor.Iftherequirementismet,amemberconstantvalueequaltrueisprovided,otherwisevalueisfalse.2)Sameas(1),butthecopyco

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/C++ : Optimization of pointers to string constants

看看这段代码:#includeusingnamespacestd;intmain(){constchar*str0="Watchmen";constchar*str1="Watchmen";char*str2="Watchmen";char*str3="Watchmen";cerr(const_cast(str0))(const_cast(str1))(str2)(str3)产生这样的输出:0x4430000x4430000x4430000x443000这是在Cygwin下运行的g++编译器上。即使没有开启优化,指针也都指向同一个位置(-O0)。编译器是否总是优化得如此之多,以至于它会

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并且通过这有效地防止我的派生类被复制? 最佳答案 尝试复制派生类时可能打印的错误消息可能是指