草庐IT

SCAN_POINTER_START

全部标签

C++ 外部 : pointer vs. 引用

我有三个类:ConsoleInputStream、ConsoleOutputStream、ConsoleErrorStream。它们都是从Stream派生的。每个流都有虚函数read和write;如您所料,当用户尝试使用ConsoleInputStream的write成员函数时,它会抛出一个错误。当用户尝试使用ConsoleOutputStream的write函数时,也会发生同样的情况。现在是显示代码的时候了。//STREAM.HPPnamespacestreamlib{externConsoleInputStreamstdin_default;externConsoleOutputS

c++ - 在这种情况下,有人可以解释 "reference"和 "pointer"之间的区别吗?

当我读到litbanswertothisquestion,我了解到通过引用传递数组可以让我们获得它的大小。我只是玩了一点代码,并尝试通过引用传递一个“函数”,令人惊讶的是(至少对我而言),这段代码编译:voidexecute(void(&func)())//funcispassedbyreference!{func();}上一个函数和这个函数有什么区别吗:voidexecute(void(*func)())//funcispassedbypointer!{func();}我用VC2008试过了,在每种情况下它都会产生不同的输出。奇怪的是编译器在函数指针的情况下更好地优化了代码:void

c++ - 使用 "pointer to volatile"是否始终阻止编译器优化?

这里是问题所在:yourprogramtemporarilyusessomesensitivedataandwantstoeraseitwhenit'snolongerneeded.使用std::fill()本身并不总是有帮助-编译器可能会决定以后不会访问内存块,因此删除它是浪费时间并消除删除代码。Userybungalobill建议usingvolatilekeyword:{charbuffer[size];//obtainandusepasswordstd::fill_n((volatilechar*)buffer,size,0);}目的是在看到volatile关键字时,编译器将不

c++ - 错误 C4703 : potentially uninitialized local pointer variable 'pNamesPtr' used

我正在做一个加密项目,在尝试编译程序时遇到了以下错误。main.cpp(520):errorC4703:potentiallyuninitializedlocalpointervariable'pNamesPtr'used==========Build:0succeeded,1failed,0up-to-date,0skipped==========DLLNAMES[i].UsedAlready=0;}*dwOutSize=(DWORD)pNamesPtr-(DWORD)pBuffer;//*有人可以帮我解决这个错误吗?您是否需要更多代码才能得到好的答案?

c++ - 哪个 Clang 警告等同于 GCC 的 Wzero-as-null-pointer-constant?

我们的项目使用C++11/14,我们希望使用nullptr而不是0或NULL指针,即使0(作为整数文字)是允许的。我有以下代码:intmain(){int*ptr1=nullptr;//#1int*ptr2=0;//#2}如果我使用GCC(5.3.0)和标记-Wzero-as-null-pointer-constant进行编译,它会在#2中发出警告,但我可以'在Clang中找不到类似的标志。如果我使用Clang(3.7.1)和标志-Weverything编译代码,我不会收到任何关于#2的警告。那么,有什么办法可以在Clang中得到类似的警告吗? 最佳答案

c++ - "The World' s Dumbest Smart Pointer 有什么意义?”

这个问题在这里已经有了答案:Useofobserver_ptr(9个回答)关闭6年前。提案N4282提倡一种称为observer_ptr的新型非拥有智能指针。它的工作名称是exempt_ptr,旨在替代“原始指针”。但我不明白它的目的,尤其是对于它旨在实现的目标的这个假设代码:structdo_nothing{templatevoidoperator()(T*){};//donothing};templateusingnon_owning_ptr=unique_ptr;即使看了文章,我也不明白什么都不做的智能指针的用途。与非拥有的shared_ptr或原始指针相比,它有什么优势?

c++ - 为什么隐含的 "lambda to function pointer conversion"禁止静态成员的 "by reference"捕获?

C++11标准说(或者至少,我拥有的版本——不是最终版本):Theclosuretypeforalambda-expressionwithnolambda-capturehasapublicnon-virtualnon-explicitconstconversionfunctiontopointertofunctionhavingthesameparameterandreturntypesastheclosuretype’sfunctioncalloperator.我理解为什么无法从有状态lambda中获取函数指针,因为函数指针本身不能保存任何数据。但是当捕获的对象只是一个静态成员/静

C# Dll 导入失败 : "The application has failed to start because its side-by-side configuration is incorrect"

我有一个c#.net4应用程序,使用vs2010。我正在尝试导入一个c++dll(基于vs2005)。[DllImport("Card.dll")]我得到了失败:UnabletoloadDLL'Card.dll':Theapplicationhasfailedtostartbecauseitsside-by-sideconfigurationisincorrect.Pleaseseetheapplicationeventlogorusethecommand-linesxstrace.exetoolformoredetail.(ExceptionfromHRESULT:0x800736B

c++ - 编译错误 : base operand of ‘->’ has non-pointer type ‘Token’

我在尝试编译我的C++代码时遇到标题中提到的错误。我无法理解我在这里做错了什么。编译器在我执行booloperator==(Token)函数时出现问题。我认为这是使运算符(operator)重载的方法。关于为什么编译器不喜欢我提到的任何线索this->terminal还是this->lexeme?classToken{public:tokenTypeterminal;std::stringlexeme;Token*next;Token();booloperator==(Token&t);private:intlexemelength,line,column;};boolToken::o

c++ - "*(pointer + integer)"在 C++ 中有什么作用?

我对这个程序中一行代码的作用感到困惑:int*temp=newint[cap];intnum=0;for(inti=name;iname、number和foo是全局变量(foo是指针),cap是一个参数。具体来说,我不明白这一行:*(temp+count)=*(foo+i);为什么会有指向括号的指针,这有什么作用? 最佳答案 *(temp+count)=*(foo+i);+运算符执行指针运算。将一个整数添加到一个指针值会产生一个新指针,该指针在原始指针之后递增了指定数量的对象。例如,如果p是指向arr[0]的指针,则p+2指向arr