草庐IT

Pointers

全部标签

c++ - 如何在 std::set<int*> 中找到 const int*?

首先,我有一套std::setmy_set;然后,我有一个函数来检查my_set中是否存在一个特定的int指针p,它只是返回true如果it指针存在于其中,否则为false。由于函数不修改被引用的int,所以很自然的把指针当作constint*,即boolexists_in_my_set(constint*p){returnmy_set.find(p)!=my_set.end();}但是,当我尝试编译代码时,出现以下错误:error:invalidconversionfrom'constint*'to'std::set::key_type{akaint*}'[-fpermissive]

c++ - 使用指向压缩结构成员的指针时出现编译器警告

许多C/C++编译器(包括gcc和clang)具有称为打包结构的功能。它派上用场的原因有很多,但必须谨慎使用。一个潜在的陷阱是您使用指向结构成员的指针作为另一个函数的参数。现在该函数不知道未对齐的指针。让我用一些代码来说明我的意思:#pragmapack(1)typedefstruct{intx;}uas;#pragmapack()voidfoo(int*f){//somecodeusingthevalueof*f}voidbar(uas*b){foo(&(b->x));}int在32位机器上的对齐方式通常是4。编译器现在可能会为foo()生成代码,如果f不是4字节对齐的。旧的ARM架

c++ - 关于指针值是编译时常量的困惑

在C++中,指针值可以是编译时常量。这是真的,否则,非类型模板参数和constexpr将无法使用指针。但是,据我所知,静态存储的函数和对象的地址(至少)在链接时而不是编译时是已知的。下面是一个例子:main.cpp#includetemplatevoidf(){std::cout();}a.cppinta=0;我只是想知道在编译main.cpp时如何知道a的地址。我希望有人能给我解释一下。特别是考虑这个templateconstexprstd::size_tf(){return(p+1)==(pp+7)?5:10;}intmain(){intarr[f()]={};}arr的存储应该如

C++ : Understanding "this" Pointer

这个问题在这里已经有了答案:InC++,whyistheaddresschangedwhenthepointerisconverted?(3个答案)关闭6年前。我想了解“this”指针。我认为“this”指针指的是类对象的值。但是,在下面的代码中,我可以看到“this”指针的不同值:#includeclassInterfaceA{public:virtualvoidfuna()=0;};classInterfaceB{public:virtualvoidfunb()=0;};voidglobala(InterfaceA*obj){printf("globalA:pointer:%p\n

C++ 使用 "new"创建静态类数组或创建动态数组的另一种方法

我知道在C++中使用new创建动态数组的常用技术是:int*arr=newint[5];一本书还说:shorttell[10];//tellisanarrayof20bytescout现在我想知道是否有一种方法可以使用new为数组分配内存,这样它就可以分配给指向整个数组的指针。它可能看起来像这样:int(*p)[5]=newint[5];上面的例子是行不通的。左边在我看来是正确的。但是我不知道右边应该是什么。我的意图是了解这是否可能。我知道有std::vector和std::array。更新:这是我真正想检查的内容:int(*p1)[5]=(int(*)[5])newint[5];//

c++ - 指针是否允许在单继承中更改值?

我知道通过多重继承,指针的值是可以改变的。但是单继承也是这样吗?还是与POD类型有关?你可能知道经典的例子:#includeusingstd::cout;structBase1{virtualvoidf1(){}};structBase2{virtualvoidf2(){}};structDerived:publicBase1,publicBase2{virtualvoidg(){}};intmain(){Derivedd{};auto*pd=&d;autopb1=(Base1*)pd;autopb2=(Base2*)pd;cout到目前为止一切顺利,这是很好的旧编译器实践。对象的布局

c++ - 获取任何 C++ 可取消引用类型的基础类型

我有一个函数可以创建P基础类型的新对象。这里的P是一个可取消引用的类型,如指针或智能指针。templateautomake_new()例如对于指针和智能指针,structA{inta=3;};A*a=make_new();std::coutab=make_new>();std::couta现在,对于共享指针,我将按如下方式实现make_new,templateautomake_new(){usingPtype=typenameP::element_type;returnP(newPtype);}这不适用于指针。现在,一些同时适用于指针和智能指针的东西,templateautomake_

c++ - C++ 中意外的指针行为

我的C++指针有问题,如果有人能够与我分享他们的专业知识,那就太好了!我得到的输出是:1:2:END:C1:C2:EEND:E我期望的输出是:1:2:END:C1:C2:CEND:E相关代码是这样的:我的测试.cpptree.insert('C');tree.insert('E');插入函数:templatepair::iterator,bool>btree::insert(constT&elem){coutrbegin_->value()node(elem);coutrbegin_->value()::iterator,bool>p(itr,false);coutrbegin_->v

c++ - 多重继承 : size of class for virtual pointers?

给定代码:classA{};classB:publicvirtualA{};classC:publicvirtualA{};classD:publicB,publicC{};intmain(){cout输出:sizeof(D)8每个类都包含自己的虚指针,但不包含其任何基类的虚指针,那么,为什么class(D)的Size是8? 最佳答案 这取决于编译器的实现。我的编译器是VisualStdioC++2005。代码如下:intmain(){cout会输出sizeof(B):4sizeof(C):4sizeof(D):8B类只有一个虚指针

c++ - 运算符== 和 list::remove()

测试.h#ifndefTEST_H#defineTEST_H#includetemplatebooloperator==(conststd::weak_ptr&wp1,conststd::weak_ptr&wp2){std::shared_ptrsp1;if(!wp1.expired())sp1=wp1.lock();std::shared_ptrsp2;if(!wp2.expired())sp2=wp2.lock();returnsp1==sp2;}#endif测试.cpp#include"Test.h"#includeintmain(){typedefstd::list>intLi