草庐IT

int_mnth_goal

全部标签

c++ - reinterpret_cast<int*>(char*) 与 static_cast<int*>(static_cast<void*>(char*)) - 使用哪个?

当你动态分配了一个char*类型的缓冲区并想将它转换为特定类型时,你是否应该使用类似的东西reinterpret_cast(char*)或者类似的东西static_cast(static_cast(char*))为什么?我个人很想使用后者,因为对我来说,它并不是真正的数据“重新解释”(而只是一种分配缓冲区的机械方式)而且它看起来不像是一个来源错误的方式可能与典型的reinterpret_cast相同,但这是正确的直觉吗? 最佳答案 AccordingtoDaveAbrahams,使用链式static_casts是强制指针类型的正确、

C++ 类型转换 int * 到类

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Regularcastvs.static_castvs.dynamic_castUndefined,unspecifiedandimplementation-definedbehavior我遇到了一个奇怪的问题。在下面的代码片段中,我定义了一个类classNewClass{public:voidTest(){cout在我的main()方法中,我写:voidmain(){int*ptr=newint();NewClass*n=((NewClass*)ptr);n->Test();}它显示“NewClassTes

c++ - 为什么我收到错误 : initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

我是C++的新手,在盯着它看了太久之后终于放弃了尝试编译它。编译器似乎出于某种原因拒绝了头文件中的构造函数原型(prototype)......我无法弄清楚它有什么问题。项目.h:#ifndefITEM_H_#defineITEM_H_classItem{public:Item(int);//ThislineiswhatEclipsekeepsflaggingupwiththeerrorinthetitlevirtual~Item();Item*getNextPtr();intgetValue();voidsetNextPtr(Item*);};#endif/*ITEM_H_*/在我的

不使用 atoi() 或 stoi() 的 C++ 字符串到 int

这个问题在这里已经有了答案:HowtoparseastringtoanintinC++?(17个答案)关闭7年前。嗨,我是C++的新手,正在尝试做一个作业,我们从一个格式为txt的文件中读取大量数据surname,initial,number1,number2在有人建议将2个值读取为字符串然后使用stoi()或atoi()转换为int之前,我寻求帮助。这很好用,除了我需要使用这个参数“-std=c++11”进行编译,否则它将返回错误。这在我自己的计算机上不是问题,它将处理“-std=c++11”,但不幸的是,对我来说,我必须在其上展示我的程序的机器没有这个选项。是否有另一种方法可以将字

firebase android碎片int null

我做FragmentViewPost在片段中,它显示出错误。但是如果我建造ViewPost不在碎片项目中它起作用。我不知道什么问题会导致片段错误java.lang.nullpointerexception:尝试调用虚拟方法'android.view.viewandroid.support.v7.widget.recyclerview.findviewbyid(int)'null对象参考这是我的代码:publicclassFragmentViewPostextendsFragment{publicstaticFragmentViewPostnewInstance(){FragmentViewPo

C++ 模板比 int->unsigned 转换更好?

我有如下两个函数templateunsignedintmyFunction(Tmyelement){myelement->func();return2;}voidmyFunction(unsignedintmyelement){}我正在使用以下代码myFunction(2);visualstudio2012提示“int没有->func()”。为什么不使用unsignedint版本? 最佳答案 您误读了错误消息。编译器不使用该函数,而是实例化它以确定它是否是候选函数。您需要禁用不适合类型的实例化:templatetypenamestd

c++ - 如何在不创建新 int 的情况下设计 RAII 文件描述符

我想围绕文件描述符创建一个RAII包装器。由于该对象可能会在线程中传递,因此它确实是一种共享资源:这就是为什么我使用带有自定义析构函数的shared_ptr进行了第一个实现。structfile_descriptor{file_descriptor(conststd::string&pathname,intflags):m_fd(initialize(pathname,flags)){}file_descriptor(constintopened_fd):m_fd(initialize(opened_fd)){}operatorint()const{return*m_fd;}priva

c++ - 转换 int -> unsigned long long 是否由标准定义

我无法在标准中找到关于如何将int值转换为unsignedlonglong的确切规范。指定了各种类似的转换,如int->unsigned、unsigned->int(UBifnegative)、unsignedlonglong->int等例如GCC,-1被转换为0xffffffffffffffff,而不是0x00000000ffffffff。我可以依赖这种行为吗? 最佳答案 是的,这个定义很明确,它基本上是将maxunsignedlonglong+1添加到-1,它始终是maxunsignedlonglong。这包含在draftC++

c++ - 我可以在多线程 C++ 中安全地使用 int 吗?

我有以下代码。有什么可以使它成为非线程安全的吗?classrunner{public:volatileintexitFlag;//constructinthreadArunner(){exitFlag=0;}//runitinthreadBvoidthreadFunc(){//doesnotmatterwhenthechangeisgettingherewhile(exitFlag==0){//...dostuff(notusingexitFlag)}}//callitfromthreadAvoidsignalThread(){exitFlag=1;//onlychangeonebit

c++ - BOOST_TYPEOF 返回 int 而不是 const int

你能解释一下为什么打印1吗?BOOST_TYPEOF不应返回constint。如何在不使用C++11功能的情况下检查函数是否返回const?#include#include#includeconstintf_const_int(){return1;}intmain(){typedefBOOST_TYPEOF(f_const_int())type;std::cout::value) 最佳答案 如果纯右值表达式的类型为cvint,则忽略该cv限定符。[表达式]/6:Ifaprvalueinitiallyhasthetype“cvT,”w