草庐IT

SOME_CONST

全部标签

c++ - 静态函数与 const 函数

我在看一个成员函数intfunct(intx)const;我想知道是否staticintfunct(intx);会更好。如果一个成员函数不使用任何成员变量,它应该是静态的吗?有什么事情会阻止这一点吗? 最佳答案 假设这是C++,声明为const的函数表示它不打算更改调用它的实例上的数据成员,即this指针。既然有办法规避这一点,这不是保证,只是声明。静态函数不对特定实例进行操作,因此不使用“this”指针。因此,它是一种非常幼稚的“const”。如果您的方法不需要绑定(bind)到特定实例,则将其设为静态是有意义的。但是,如果您的方

c++ - 按分配给 const 引用的值返回

我正在修复一些代码中的另一个错误,并遇到了一些我认为是错误的代码;但是,此代码在gcc4.4、4.5和4.6下编译,并且似乎按“预期”运行。谁能告诉我这是否是有效的c++?structfoo{intbar;};foomyfunction(fooconst&orig){foofooOnStack=orig;fooOnStack.bar*=100;returnfooOnStack;}voidmyOtherFunction(fooconst&orig){fooconst&retFoo=myfunction();//perhapsdosometestsonretFoo.bar...}如果这是有

c++ - 按分配给 const 引用的值返回

我正在修复一些代码中的另一个错误,并遇到了一些我认为是错误的代码;但是,此代码在gcc4.4、4.5和4.6下编译,并且似乎按“预期”运行。谁能告诉我这是否是有效的c++?structfoo{intbar;};foomyfunction(fooconst&orig){foofooOnStack=orig;fooOnStack.bar*=100;returnfooOnStack;}voidmyOtherFunction(fooconst&orig){fooconst&retFoo=myfunction();//perhapsdosometestsonretFoo.bar...}如果这是有

c++ - 如何返回不可用的 const 引用

假设我有以下功能:conststd::string&Cat::getKittenName()const{Kitten*kitty=getKitty();returnkitty->getName();}Kitten::getName返回conststd::string&我如何最好地处理kitty是的情况空指针?我可以返回std::string("")但随后我返回对临时且实际上保证未定义行为的引用。我可以更改getKittenName函数以返回一个std::string来解决这个问题,但是我会为kitty可用。现在我觉得最好的选择是:conststd::string&Cat::getKit

c++ - 如何返回不可用的 const 引用

假设我有以下功能:conststd::string&Cat::getKittenName()const{Kitten*kitty=getKitty();returnkitty->getName();}Kitten::getName返回conststd::string&我如何最好地处理kitty是的情况空指针?我可以返回std::string("")但随后我返回对临时且实际上保证未定义行为的引用。我可以更改getKittenName函数以返回一个std::string来解决这个问题,但是我会为kitty可用。现在我觉得最好的选择是:conststd::string&Cat::getKit

c++ - "const T*"可以匹配指向自由函数的指针吗?

在relatedquestion中,据说没有指向非成员const函数的指针。另外,C++118.3.5/6说Theeffectofacv-qualifier-seqinafunctiondeclaratorisnotthesameasaddingcv-qualificationontopofthefunctiontype.Inthelattercase,thecv-qualifiersareignored.[Note:afunctiontypethathasacv-qualifier-seqisnotacv-qualifiedtype;therearenocv-qualifiedfun

c++ - "const T*"可以匹配指向自由函数的指针吗?

在relatedquestion中,据说没有指向非成员const函数的指针。另外,C++118.3.5/6说Theeffectofacv-qualifier-seqinafunctiondeclaratorisnotthesameasaddingcv-qualificationontopofthefunctiontype.Inthelattercase,thecv-qualifiersareignored.[Note:afunctiontypethathasacv-qualifier-seqisnotacv-qualifiedtype;therearenocv-qualifiedfun

c++ - 是否应该通过 const 引用传递小的简单结构?

我一直被教导非原始类型应该通过const引用而不是在可能的情况下通过值传递,即:voidfoo(std::stringstr);//badvoidfoo(conststd::string&str);//good但我今天在想,也许实际上一些简单的用户定义类型实际上可能更好地按值传递,例如:classVector2{public:floatx,y;...constructors,operatorsoverloads,utilitymethods,etc...};voidfoo(Vector2pos);voidfoo(constVector2&pos);//isthisreallybette

c++ - 是否应该通过 const 引用传递小的简单结构?

我一直被教导非原始类型应该通过const引用而不是在可能的情况下通过值传递,即:voidfoo(std::stringstr);//badvoidfoo(conststd::string&str);//good但我今天在想,也许实际上一些简单的用户定义类型实际上可能更好地按值传递,例如:classVector2{public:floatx,y;...constructors,operatorsoverloads,utilitymethods,etc...};voidfoo(Vector2pos);voidfoo(constVector2&pos);//isthisreallybette

c++ - 为什么Visual C++在C中而不是从C++中警告从const void **到void *的隐式强制转换?

概要当C程序试图将指向const数据的指针(如constvoid**或constchar**)转换为void*时,MicrosoftVisualStudio中的C/C++编译器会给出警告C4090(即使这种类型实际上并非指向const的指针)。更奇怪的是,同一个编译器静默地接受作为C++编译的相同代码。这种不一致的原因是什么?为什么VisualStudio(与其他编译器不同)在将指向const的指针隐式转换为void*时存在问题?细节我有一个C程序,其中将通过变量参数列表传递的C字符串读入数组(通过调用va_arg的循环)。由于C字符串的类型为constchar*,因此跟踪它们的数组的