草庐IT

c++ - 构造函数中指针的默认值是什么意思?

我试着理解这段代码(取自here):templateclassauto_ptr{T*ptr;public:explicitauto_ptr(T*p=0):ptr(p){}~auto_ptr(){deleteptr;}T&operator*(){return*ptr;}T*operator->(){returnptr;}//...};我无法理解这行代码:explicitauto_ptr(T*p=0):ptr(p){}。据我所知,在这一行中,我们尝试定义一个构造函数,该构造函数具有一个pointer-to-object-of-T-class类型的参数。然后我们有=0。那是什么?它是默认值吗

c++ - 使用 dynamic_pointer_cast 时无法动态转换

为什么这段代码不起作用?std::shared_ptre=ep->pop();std::shared_ptrt;t=std::dynamic_pointer_cast(e);我收到以下错误:/usr/include/c++/4.6/bits/shared_ptr.h:386:error:cannotdynamic_cast'(&__r)->std::shared_ptr::.std::__shared_ptr::get[with_Tp=Event,__gnu_cxx::_Lock_policy_Lp=(__gnu_cxx::_Lock_policy)2u]()'(oftype'clas

c++ - 视觉 C++ 2008 : debugging data behind pointer array

如何在VisualStudio2008中查看数组指针后面的数据而不是第一项?如果能看到任意数量的项目,而不仅仅是第一个,那将非常有用。 最佳答案 char*p=newchar[100];在监window口中输入:p,100 关于c++-视觉C++2008:debuggingdatabehindpointerarray,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3659108/

c++ - 为什么可以将指针与 '\0' 进行比较? (但不是 'A' )

我在我的代码中发现了一个错误,我将指针与“\0”进行了比较。想知道为什么编译器没有警告我这个错误,我尝试了以下方法。#includestructFoo{charbar[5];};intmain(){Foof;Foo*p=&f;p->bar[0]='\0';assert(p->bar=='\0');//#1.Iforgot[]Now,comparingpointerwithNULLandfails.assert(p->bar=='A');//#2.error:ISOC++forbidscomparisonbetweenpointerandintegerassert(p->bar[0]==

c++ - 函数参数 : Pointer to array of objects

在我的主要功能中,我创建了一个特定类“菜单”的对象数组当我调用一个函数时,我想提供一个指向该数组的指针。Menumenu[2];//Createmenu[0],[1]Function(POINTER_TO_ARRAY);问题:函数参数的正确写法是什么?我尝试:Function(&menu);在头文件中:voidFunction(Menu*menu[]);//notworkingerror:Cannotconvertparameter1fromMenu(*)[2]toMenu*[]voidFunction(Menu*menu);//notworkingerror:Cannotconver

c++ - std::static_pointer_cast 与 static_cast<std::shared_ptr<A>>

我有一个类层次结构,其中B源自A像这样:classA:publicstd::enable_shared_from_this{};classB:publicA{voidf(){//thecodebelowcompilesstd::shared_ptrcopyOfThis=std::static_pointer_cast(shared_from_this());//thecodebelowdoesnotstd::shared_ptrcopyOfThis=static_cast>(std::make_shared(shared_from_this()));}};所以实际上我想了解为什么我不能

C++11 decltype : How to declare the type that a pointer points to?

我有以下代码:#includeintmain(){int*a=newint(2);std::unique_ptrp(a);}导致这些错误信息:Infileincludedfroma.cpp:1:Infileincludedfrom/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/

C++ is_member_pointer 实现

在c++标准库中,is_member_pointer实现为templatestruct__is_member_pointer_helper:publicfalse_type{};templatestruct__is_member_pointer_helper:publictrue_type{};///is_member_pointertemplatestructis_member_pointer:public__is_member_pointer_helper::type>::type{};有人可以解释一下_Cp是如何推导出来的吗?它像魔术一样工作。 最佳答

c++ - C 和 C++ : Array element access pointer vs int

如果您执行myarray[i]或将myarray[i]的地址存储在指针中,是否存在性能差异?编辑:这些指针都是在我的程序中一个不重要的步骤中计算出来的,性能不是标准。在关键部分,指针保持静态并且不被修改。现在的问题是这些静态指针是否比一直使用myarray[i]更快。​​ 最佳答案 对于这段代码:intmain(){inta[100],b[100];int*p=b;for(unsignedinti=0;i在g++中使用-O3优化构建时,语句:a[i]=i;产生汇编输出:mov%eax,(%ecx,%eax,4)和这个声明:*p++=

c++ - 在 C++ 中递增常量

有人可以向我解释为什么这段代码有效吗?我觉得编译器不应该允许我做我所做的事情(移动一个int指针指向一个constint),或者我至少会期待编译器警告或段错误。改变常量值的想法似乎是错误的。代码:#includeusingnamespacestd;structtest_struct{inti;constintj;};intmain(){cout输出:Createastructwithinti=100andconstintj=101.100101Createpointerpandpointittointi.100Incrementpointerp,whichshouldnowbepoin