草庐IT

const-char

全部标签

c++ - const char * 与其他指针不同吗?

这个问题在这里已经有了答案:Whydoescoutprintchararraysdifferentlyfromotherarrays?(4个答案)关闭4年前。所以我最近一直在深入研究指针和引用,因为它们经常出现在我用来学习OpenGL的资源中。我注意到constchar*指针的行为似乎与其他指针有些不同。为了演示,我创建了这个测试程序:#includeintmain(){inti=2;int*pi;//constcharc="helloworld";constchar*pc="helloworld";pi=&i;std::cout输出:'helloworld'typeischarcon

c++ - 我可以用三元初始化一个 char[] 吗?

我问了aquestionaboutit并没有得到一个真正明确的答案,但在阅读之后thisarticle我开始更喜欢constchar[]而不是constchar*。我在用三元初始化时遇到了困难。给定constboolbar,我试过:constcharfoo[]=bar?"lorem":"ipsum"这给了我错误:error:initializerfailstodeterminesizeoffooconstcharfoo[]=bar?{'l','o','r','e','m','\0'}:{'i','p','s','u','m','\0'这给了我错误:error:expectedprima

c++ - 如何将 wchar_t** 转换为 char**?

我得到argv作为wchar_t**(见下文),因为我需要使用unicode,但我需要将它转换为char**。我该怎么做?intwmain(intargc,wchar_t**argv){ 最佳答案 有不止一种方法可以做到这一点。根据您的环境和可用的编译器/标准库/其他库,您至少有三种选择:使用std::locale和std::codecvt方面;使用C语言环境函数,如std::mbstowcs();使用第3方函数,例如*nix上的iconv()或Windows上的WideCharToMultiByte()。您真的需要进行转换吗?您应

c++ - 什么时候删除由 (char *) 作为函数参数指向的内存?

代码1:voidfoo(char*text){}foo("Test");据我所知,这将会发生:内存分配给“测试”指针被创建并将其值复制到(char*textpointer),因此(char*text)指向内存中的位置,其中“Test”是(最好说,在第一个字符上"测试")函数执行完后,销毁了指向“Test”开头的指针(char*text),这不是内存泄漏吗?问题是,“Test”什么时候被删除,什么时候函数只销毁指针做某事不是更好吗?像那样?:char*_text="Test";foo(_text);delete[]_text; 最佳答案

c++ - 如何从成员函数模板类型签名中删除 const?

我正在研究一些C++类型系统的东西,但我在从成员函数中删除const-ness以用于函数特征类时遇到问题。真正麻烦的是,这在G++中运行良好,但MSVC10无法正确处理部分特化,我不知道这些编译器中的一个是否真的存在错误。这里的问题是,以我可以获得函数类型签名的方式从成员函数中删除const限定符的正确方法是什么?采用以下代码示例:#includetemplatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedef

c++ - 将临时对象附加到对 const 的引用时出错

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:typedefandcontainersofconstpointers为什么代码会发出错误?intmain(){//testcodetypedefint&Ref_to_int;constRef_to_intref=10;}错误是:error:invalidinitializationofnon-constreferenceoftype‘int&’fromatemporaryoftype‘int’我阅读了prolongingthelifetimeoftemporaries上的帖子这表示临时对象可以绑定(bind

c++ - 将 char 数组的最后 8 个字节设置为 __uint64

我正在通过维基百科上的伪代码实现SHA1算法。它说我应该将原始长度作为64位附加到消息中,所以我尝试了以下操作://new_messageisoftypechar[]andis9+byteslong*((__int64*)(new_message-8))=(__int64)length;这会导致new_message的内存损坏。有人能发现错误吗?谢谢!编辑:天哪,我太傻了。new_message指向我数组的开头,难怪它崩溃了! 最佳答案 new_message-8将从当前指针返回8个字节,这是意图吗?append意味着在末尾添加8个

c++ - 是否可以编写这些 pure_assert 和 const_assert 宏?

GCC__attribute__((pure))和__attribute__((const))分别允许将函数声明为无副作用和引用透明;假设我想编写pure_assert和const_assert宏,其参数必须是适当严格级别的表达式,即:assert(oops_a_side_effect());静默导致调试和发布中的不同行为,但是:pure_assert(oops_a_side_effect());const_assert(oops_read_a_global());至少在调试版本中会出现编译时错误。由于我希望是显而易见的原因,您不能只创建一个声明为__attribute__((pure

c++ - const 引用右值的类数据成员的生命周期是多少?

通常这个讨论只针对局部函数变量:voidfoo(constint&i){//useitillfoo()ends}foo(3);但是,这条规则是否也适用于class成员?structA{constint&a;A():a(3){}//version1A(constint&i):a(i){}//version2};现在A用作,{return()?newA:newA(3):newA(some_local_variable);}a的内容是否会在all3的整个生命周期内保持不变新分配A? 最佳答案 C++03标准(“12.2/5临时对象”部分)

c++ - 指向 const 类型的 const 指针的模板特化

我正在阅读http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/并遇到这段代码来检查类型是否为指针:templatestructisPtr{staticconstboolvalue=false;};templatestructisPtr{staticconstboolvalue=true;};templatestructisPtr{staticconstboolvalue=true;};我如何专门化通用模板来处理指向const类型的const指针的情况?如果我这样做:std