草庐IT

const_buffers

全部标签

c++ - 如何避免 const cast 进行 map 访问?

我有以下问题:std::mapmap;voidgetColor(Aconst*obj){doubled=map[obj];//doesnotcompilewihtoutconst_cast(obj)//dosomething}我有一个mapstd::map(某处),它存储指向对象A的指针。我有一个getColor函数,它不操作对象A,因此将指向constA的指针作为输入.如果不使用const_cast,函数getColor将无法编译。constcast是一个设计问题,但如果我不想在map中制作键,我不知道如何规避它常量。任何帮助表示赞赏。 最佳答案

c++ - 如何在 C++ 中将 auto 与 const 和 & 一起使用?

我有一个返回constA&的方法。如果我想使用自动,那么正确的方法是什么。这样可以吗?constauto&items=someObject.someMethod();我看到有些人这样做:auto&items=someObject.someMethod();我不确定该使用哪一个,真正的区别是什么......编辑:在这种情况下,这两个是等价的吗?autoitems=someObject.someMethod();auto&items=someObject.someMethod(); 最佳答案 即使这两种形式是等价的在这种情况下,我还是会

c++ - 如何在 C++ 中将 auto 与 const 和 & 一起使用?

我有一个返回constA&的方法。如果我想使用自动,那么正确的方法是什么。这样可以吗?constauto&items=someObject.someMethod();我看到有些人这样做:auto&items=someObject.someMethod();我不确定该使用哪一个,真正的区别是什么......编辑:在这种情况下,这两个是等价的吗?autoitems=someObject.someMethod();auto&items=someObject.someMethod(); 最佳答案 即使这两种形式是等价的在这种情况下,我还是会

c++ - 我可以从现在开始创建一个变量 _const 吗?

我正在使用一个库,该库的类具有一个不同于其构造函数的init函数。每次我创建一个新实例时,我都需要调用,例如:MyClassa;a.init();由于init不是const,这会阻止我创建const实例(我无法编写constMyClassa)。有什么方法可以调用init然后从“hereonout”声明(我猜对于范围的其余部分)我的变量是const?这可行,但依赖于不触及原始变量:MyClassdont_touch;dont_touch.init();constMyClass&a=dont_touch; 最佳答案 如果你使用C++11

c++ - 我可以从现在开始创建一个变量 _const 吗?

我正在使用一个库,该库的类具有一个不同于其构造函数的init函数。每次我创建一个新实例时,我都需要调用,例如:MyClassa;a.init();由于init不是const,这会阻止我创建const实例(我无法编写constMyClassa)。有什么方法可以调用init然后从“hereonout”声明(我猜对于范围的其余部分)我的变量是const?这可行,但依赖于不触及原始变量:MyClassdont_touch;dont_touch.init();constMyClass&a=dont_touch; 最佳答案 如果你使用C++11

c++ - 如何在 C++ 中定义一个 const 指针数组?

有没有办法定义一个指针数组,使任何指针都是const的?例如,是否可以将char**数组定义为array[0]为const而array[1]为const,等等,但是array是非常量并且array[j][i]是非常量? 最佳答案 char*const*指针;。那么pointer->non-constpointertoconstpointertonon-constchar(char*const*)pointer[0]->constpointertonon-constchar(char*const)pointer[0][0]->non-

c++ - 如何在 C++ 中定义一个 const 指针数组?

有没有办法定义一个指针数组,使任何指针都是const的?例如,是否可以将char**数组定义为array[0]为const而array[1]为const,等等,但是array是非常量并且array[j][i]是非常量? 最佳答案 char*const*指针;。那么pointer->non-constpointertoconstpointertonon-constchar(char*const*)pointer[0]->constpointertonon-constchar(char*const)pointer[0][0]->non-

c++ - 如何在 const 函数中调用非常量函数 (C++)

我有一个看起来像这样的旧函数:intRandom()const{returnvar_?4:0;}我需要在该遗留代码中调用一个函数,使其现在看起来像这样:intRandom()const{returnvar_?newCall(4):0;}问题是我收到了这个错误:Inmemberfunction'virtualintRandom()const':class.cc:145:error:passing'constint'as'this'argumentof'intnewCall(int)'discardsqualifiers现在我知道为了修复这个错误,我可以让我的newCall()成为一个co

c++ - 如何在 const 函数中调用非常量函数 (C++)

我有一个看起来像这样的旧函数:intRandom()const{returnvar_?4:0;}我需要在该遗留代码中调用一个函数,使其现在看起来像这样:intRandom()const{returnvar_?newCall(4):0;}问题是我收到了这个错误:Inmemberfunction'virtualintRandom()const':class.cc:145:error:passing'constint'as'this'argumentof'intnewCall(int)'discardsqualifiers现在我知道为了修复这个错误,我可以让我的newCall()成为一个co

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();