草庐IT

const修饰符的增加

全部标签

c++ - 写 foo(const float&) 是在浪费精力吗?

当传递像int或float这样的原始类型时,这样写是不是浪费了精力:foo(constfloat&);而不只是按值传递:foo(float); 最佳答案 为了花车?Yes,prettymuch.这里根本没有任何好处:float很小,复制不会比创建指针来实现引用慢。 关于c++-写foo(constfloat&)是在浪费精力吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/210

c++ - unordered_map<const T, int> 和 map<const T, int> 的区别

#include#include#includeusingnamespacestd;classSolution{public:private://unordered_mapmapStrInt;//Case1:OK//unordered_mapmapStrInt;//Case2:Fail//mapmapStrInt;//Case3:OK//mapmapStrInt;//Case4:OK};问题>为什么Case2不合法?template,//unordered_map::hasherclassPred=equal_to,//unordered_map::key_equalclassAllo

c++ - 为什么我不能用 const_iterator 调用模板基类构造函数?

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。由于某些原因,下面的代码给出了错误Symbol'TemplateBase'couldnotberesolved.:templateclassTemplateBase{TemplateBase(std::map::const_iteratoranIterator){}};classSubClass:publicTemplateBase{SubClass(std::map::const_iteratoranIterator)

c++ - 定期使用 const_cast 作为设计工具是否可以接受?

我查看了下面的代码类型,虽然我对问题(*)有个人答案,但我希望得到C++/设计专家的评论。出于某种原因,Data是一个具有不可修改标识符和可修改值的对象:classData{constIdm_id;//设计选择变成了语言选择,因为标识符在类级别(**)被声明为const,以避免它的(意外)修改,即使是在类成员函数内部.........但是如您所见,有一个复制赋值运算符,其实现方式为:Data&Data::operator=(constData&that){if(this!=&that){const_cast(this->m_id)=that.m_id;this->m_value=tha

c++ - 如何找到应该是 const 的 C++ 函数?

我有这个代码:#includeclassA{public:intdoit(){return5;}intdoit2()const{i++;returni;}inti;};intmain(){Aa;printf("%d\n",a.doit());return0;}使用g++-Wall-Wpedanticmain.cpp可以干净地编译。有没有办法让g++说“A::doit()应该标记为const”?g++4.8有-Wsuggest-attribute=const但在这种情况下它似乎不起作用。g++-Wall-Wpedantic-Wsuggest-attribute=constconst_ma

c++ - 如何干净地使用:const char* 和 std::string?

tl:drHowcanIconcatenateconstchar*withstd::string,neatlyandelegantly,withoutmultiplefunctioncalls.Ideallyinonefunctioncallandhavetheoutputbeaconstchar*.Isthisimpossible,whatisanoptimumsolution?初始问题到目前为止,我在C++中遇到的最大障碍是它如何处理字符串。在我看来,在所有广泛使用的语言中,它处理字符串的能力最差。我见过其他与此类似的问题,这些问题的答案要么是“使用std::string”,要么只

c++ - Typedef、模板和 const 关键字

我在使用带有const关键字(用于函数参数类型)的模板时遇到问题,为了说明这一点,我创建了一个小代码:templatestructMethodCallerFactory{typedefReturnType(*Type)(ClassType*,Args...);templatestructMethod{staticTypecreateMethodCaller(){ReturnType(*caller)(ClassType*,Args...)=[](ClassType*obj,Args...args)->ReturnType{ReturnType(ClassType::*ptr)(Args

c++ - 这是 `` const_cast`` 的有效用法吗?

C++11标准更改了erase()的签名标准容器的方法:他们现在接受const_iterators而不是iterator秒。本文档解释了基本原理:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf现在,如果一个人执行std::vector直接用constT*就可以了和T*分别作为常量和可变迭代器类型。所以在erase()方法我们可能有这样的代码:iteratorerase(const_iteratorit){...for(;it!=end()-1;++it){//Destroythecurrenteleme

c++ - 内联调用不太可能失败,代码大小会增加 [-Winline] 但不使用内联

对C++很陌生。这是我的用户定义的fmiNode类:(fmi.h)classfmiNode{public:fmiNode(std::stringNodeName,intAddress){this->name=NodeName;this->address=Address;}std::stringGetName(){returnthis->name;}intGetAddress(){returnthis->address;}private:std::stringname;intaddress;};这是我的主要方法(fmi.c)intmain(intargc,char*argv[]){fmi

c++以对数方式增加for循环增量

我想像这样遍历整数:1,2,3,4,5,6,7,8,9,10,20,30,40,...,100,200,...,1000,2000,...我有执行此操作的代码(如下所示),但它很麻烦,而且通常没有编程来处理不同的停止限制:intMAX=10000;for(inti=1;i=10&&i=100&&i=1000&&i如您所见,这是如前所述指定的情况-所以我想知道一种以更通用的方式对此进行编码的方法,至于我的要求MAX将是10^9的数量级所以使用上面的代码太不切实际了。 最佳答案 试试这段代码。它更通用:intMAX=1000000;fo