草庐IT

const_buffers

全部标签

c++ - 如何使用 Google Protocol Buffer 序列化为 char*?

我想将我的ProtocolBuffer序列化为char*。这可能吗?我知道可以按照以下方式序列化到文件:fstreamoutput("/home/eamorr/test.bin",ios::out|ios::trunc|ios::binary);if(!address_book.SerializeToOstream(&output)){cerr但我想序列化为C风格的char*以便通过网络传输。如何做到这一点?请记住,我对C++很陌生。 最佳答案 这很简单:size_tsize=address_book.ByteSizeLong();

c++ - 我可以在 C++ 运行时初始化静态 const 成员吗?

是否可以在运行时初始化我的类的静态const成员?这个变量在我的程序中是一个常量,但我想将它作为命令行参数发送。//A.hclassA{public:staticconstintT;};//inmainmethodintmain(intargc,char**argv){//howcanIdosomethinglikeA::T=atoi(argv[1]);}如果无法做到这一点,我应该使用什么类型的变量?我需要在运行时初始化它并保留常量属性。 最佳答案 您不能依赖main开始后生成的数据来初始化static变量,因为在main的翻译单元

c++ - 我可以在 C++ 运行时初始化静态 const 成员吗?

是否可以在运行时初始化我的类的静态const成员?这个变量在我的程序中是一个常量,但我想将它作为命令行参数发送。//A.hclassA{public:staticconstintT;};//inmainmethodintmain(intargc,char**argv){//howcanIdosomethinglikeA::T=atoi(argv[1]);}如果无法做到这一点,我应该使用什么类型的变量?我需要在运行时初始化它并保留常量属性。 最佳答案 您不能依赖main开始后生成的数据来初始化static变量,因为在main的翻译单元

c++ - 具有 const 和 nonconst 成员的 union ?

这似乎是未定义的行为unionA{intconstx;floaty;};Aa={0};a.y=1;规范说Creatinganewobjectatthestoragelocationthataconstobjectwithstatic,thread,orautomaticstoragedurationoccupiesor,atthestoragelocationthatsuchaconstobjectusedtooccupybeforeitslifetimeendedresultsinundefinedbehavior.但是没有编译器会警告我,因为它很容易诊断错误。我是否误解了措辞?

c++ - 具有 const 和 nonconst 成员的 union ?

这似乎是未定义的行为unionA{intconstx;floaty;};Aa={0};a.y=1;规范说Creatinganewobjectatthestoragelocationthataconstobjectwithstatic,thread,orautomaticstoragedurationoccupiesor,atthestoragelocationthatsuchaconstobjectusedtooccupybeforeitslifetimeendedresultsinundefinedbehavior.但是没有编译器会警告我,因为它很容易诊断错误。我是否误解了措辞?

c++ - 我应该返回 bool 还是 const bool?

哪个更好:boolMyClass::someQuery()const;constboolMyClass::someQuery()const;我一直在使用“constbool”,因为我确定我记得听到它是“int的作用”(例如比较运算符),但我无法在任何地方找到证据,主要是因为它很难对Google和Intellisense没有任何帮助;)有人可以确认吗?对我来说,返回const值(这不仅仅是关于bool值)更有意义;它将防止临时变量被修改,这几乎总是程序员的错误。我只是想要一些东西来支持它,这样我就可以向我的同事赞美返回const值:) 最佳答案

c++ - 我应该返回 bool 还是 const bool?

哪个更好:boolMyClass::someQuery()const;constboolMyClass::someQuery()const;我一直在使用“constbool”,因为我确定我记得听到它是“int的作用”(例如比较运算符),但我无法在任何地方找到证据,主要是因为它很难对Google和Intellisense没有任何帮助;)有人可以确认吗?对我来说,返回const值(这不仅仅是关于bool值)更有意义;它将防止临时变量被修改,这几乎总是程序员的错误。我只是想要一些东西来支持它,这样我就可以向我的同事赞美返回const值:) 最佳答案

c++ - 为什么 main() 参数 argv 的类型是 char*[] 而不是 const char*[]?

当我写了下面的代码并执行它时,编译器说deprecatedconversionfromstringconstanttochar*intmain(){char*p;p=newchar[5];p="howareyou";cout这意味着我应该写constchar*。但是当我们使用char*argv[]将参数传递到main时,我们不要编写constchar*argv[]。为什么? 最佳答案 因为...argv[]不是const。而且它肯定不是(静态)字符串文字,因为它是在运行时创建的。您正在声明一个char*指针,然后为其分配一个字符串文

c++ - 为什么 main() 参数 argv 的类型是 char*[] 而不是 const char*[]?

当我写了下面的代码并执行它时,编译器说deprecatedconversionfromstringconstanttochar*intmain(){char*p;p=newchar[5];p="howareyou";cout这意味着我应该写constchar*。但是当我们使用char*argv[]将参数传递到main时,我们不要编写constchar*argv[]。为什么? 最佳答案 因为...argv[]不是const。而且它肯定不是(静态)字符串文字,因为它是在运行时创建的。您正在声明一个char*指针,然后为其分配一个字符串文

c++ - 在 C 和 C++ 中使用 const 限定符指向数组的指针

考虑以下程序:intmain(){intarray[9];constint(*p2)[9]=&array;}它在C++中编译良好(参见现场演示here),但在C中编译失败。默认情况下,GCC会给出以下警告。(见现场演示here)。prog.c:Infunction'main':prog.c:4:26:warning:initializationfromincompatiblepointertype[enabledbydefault]constint(*p2)[9]=&array;但如果我使用-pedantic-errors选项:gcc-Os-s-Wall-std=c11-pedanti