草庐IT

const_buffers

全部标签

c++ - const_cast 不适用于 C++?

这个问题在这里已经有了答案:Twodifferentvaluesatthesamememoryaddress(7个答案)关闭5年前。我有以下代码:constintk=1;int*p=const_cast(&k);cout(&k)=12;cout输出是:kbefore=1kafter=1为什么constcast在这里不起作用?

c++ - std::is_const 将 const 指针标识为非常量

我很困惑std::is_const识别const的行为指针为非const.我自己的实现is_const做完全一样的事情。我不确定为什么更通用的模板化结构正在挑选版本。gcc4.7和clang3.1-svn表现出相同的行为。任何人都可以解释发生了什么事吗?代码如下:#include#include#includeclassCEmptyClass{};namespacejbc{templatestructis_const:std::false_type{};templatestructis_const:std::true_type{};}intmain(intargc,char*argv[

c++ - 为什么当 typedef const 指针与 extra const 一起使用时编译器不报错?

以下给出了预期的错误:int*constconstp=newint;//g++error:duplicatecv-qualifier但下面没有给出任何错误,即使它等同于上面的错误:typedefint*constintp_const;intp_constconstp=newint;//ok!//^^^^^duplicate?为什么编译器会忽略额外的const?[注:intp_constconst与constchar*const不同,因为*p=;是可能的。] 最佳答案 在7.1.5[dcl.type](C++03)中,规定在通过typ

c++ - 指向 const 的指针与通常的指针(对于函数)

指向const的指针和通常的函数指针之间有什么区别吗?什么时候适合对独立函数使用const限定符?我写了简短的示例来说明我的问题:#includeusingnamespacestd;intsum(intx,inty){returnx+y;}typedefintsum_func(int,int);intmain(){constsum_func*sum_func_cptr=∑//constfunctionsum_func*sum_func_ptr=∑//non-constfunction?//Whatisthedifferencebetweensum_func_cptran

c++ - 将 System::String 转换为 Const Char *

这个问题在这里已经有了答案:howtoconvertSystem::Stringtoconstchar*?(2个答案)关闭7年前。我正在使用VisualC++2008的GUI创建器制作用户界面。单击按钮时,将调用以下函数。内容应该创建一个文件,并以文本框“文本框”的内容命名该文件,末尾带有“.txt”。但是,这导致我出现转换错误。这是代码:私有(private):System::VoidButton_Click(System::Object^sender,System::EventArgs^e){ofstreammyfile(Textbox->Text+".txt");我的文件.clo

c++ - 在一个 Protocol Buffer 二进制文件中存储多条消息

我有重复的消息,我想将它们存储在一个文件中。目前我必须将这条重复的消息包装在另一条消息中。有解决办法吗?packagefoo;messageBox{requiredint32tl_x=1;requiredint32tl_y=2;requiredint32w=3;requiredint32h=4;}messageBoxes{repeatedBoxboxes=1;} 最佳答案 这是"Techniques"ProtocolBuffers文档的一部分提到了重复消息:Ifyouwanttowritemultiplemessagestoasin

c++ - const lambda 是什么意思?

#includeintfoo(inti){constautoa=[&i](){i=7;returni*i;};a();returni;}intmain(){std::cout这会编译(g++-std=c++11-Wall-Wextra-Wpedanticmain.cpp)并返回49。这让我感到惊讶,因为通过将a声明为常量对象,我会期望i被引用为constint&。显然不是,为什么? 最佳答案 Lambda就像非lambda一样,除了它们的实现细节是隐藏的。因此,使用非lambda仿函数可能更容易解释:#includeintfoo(i

c++ - 为什么通过 int& 捕获 const int& 有效?

在下面的代码中,我抛出一个int,将其作为constint&捕获,重新抛出并再次捕获它,将其作为int&捕获。#includeintmain(){try{try{intx=1;throwx;}catch(constint&e){std::cout以上程序编译成功并打印InnercatchOutercatch另一方面,我试图通过constint&初始化int&的以下程序甚至无法编译。#includeintmain(){intx=0;constint&y=x;int&z=yreturn0;}我得到了预期的以下错误binding‘constint’toreferenceoftype‘int&

c++ - 海湾合作委员会错误 : invalid conversion from double* to const double

我使用的是gcc4.5.0版。使用下面的简单示例,我假设得到一个错误invalidconversionfromdouble*toconstdouble*#includeusingnamespacestd;voidfoo(constdouble*a){cout为什么编译没有错误?类似的反例如下:#includeusingnamespacestd;voidfoo(constdouble**a){cout(第二个示例的解决方案:将foo定义为foo(constdouble*const*a)。感谢JackEdmonds的评论,这解释了错误消息) 最佳答案

c++ - 为什么要使用 const 成员函数?

我正在尝试理解类getter和setter函数...我的问题是:如果我设计的函数只从其类中获取状态(“getter”函数),为什么将其标记为“const成员函数”?我的意思是,如果我的函数旨在不更改其类的任何属性,为什么还要使用const成员函数?我不明白请:(例如:intGetValue(){returna_private_variable;}和intGetValue()const{returna_private_variable;}真正的区别是什么? 最佳答案 当您将成员函数声明为const时,例如intGetValue()co