jjzjj

C++ 外部 : pointer vs. 引用

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

c++ - 在指向 T 的指针、T 的数组和指向 T 的数组的指针之间进行转换是否有未定义的行为?

考虑以下代码。#includeintmain(){typedefintT;Ta[]={1,2,3,4,5,6};T(*pa1)[6]=(T(*)[6])a;T(*pa2)[3][2]=(T(*)[3][2])a;T(*pa3)[1][2][3]=(T(*)[1][2][3])a;T*p=a;T*p1=*pa1;//T*p2=*pa2;//errorinc++//T*p3=*pa3;//errorinc++T*p2=**pa2;T*p3=***pa3;printf("%p%p%p%p%p%p%p\n",a,pa1,pa2,pa3,p,p1,p2,p3);printf("%d%d%d%d%

c++ - 为什么C++允许从外部修改一个常量对象的指针成员变量的内存?

当我在C++中编写一个带有常量参数和该对象内部的指针变量的函数时,我一直在努力理解,而不是const标志不保护底层内存免受修改。例如,在名为X的类的operator=()函数中执行以下操作是完全合法的:classX{public:X&operator=(constX&other){this->data=other.data;//(*)return*this;}private:int*data;};(*):这与以下相同:int*some_pointer;int*constother_pointer=some_pointer;int*class_pointer=other_pointer;

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++ - static_cast 从基析构函数到指向派生类的指针的安全性

这是问题的变体DowncastingusingtheStatic_castinC++和Safetyofinvaliddowncastusingstatic_cast(orreinterpret_cast)forinheritancewithoutaddedmembers关于~B中的行为,我不清楚标准中的短语“B实际上是D类型对象的子对象,结果指针指向D类型的封闭对象”。如果在~B中转换为D,此时它仍然是子对象吗?以下简单示例显示了问题:voidf(B*b);classB{public:B(){}~B(){f(this);}};classD:publicB{public:D(){}};s

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++ - 从 int 到 shared_ptr 的隐式转换

考虑下面的代码:#include#includevoidf(std::shared_ptrsp){}templateautocall_f(FuncTypef,PtrTypep)->decltype(f(p)){returnf(p);}intmain(){f(0);//doesn'tworkforanyotherint!=0,thanks@Rupesh//call_f(f,0);//error,cannotconvertinttoshared_ptr}在main()中的第一行,整数0转换为std::shared_ptr和电话f(0)成功没有任何问题。但是,使用模板调用函数会使情况有所不同

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或原始指针相比,它有什么优势?