草庐IT

constness

全部标签

c++ - 为什么可以在构造函数中修改 const 成员?

我很好奇为什么可以在构造函数中修改const成员。初始化中是否有任何标准规则可以覆盖成员的“常量”?structBar{constintb=5;//defaultmemberinitializationBar(intc):b(c){}};Bar*b=newBar(2);//Problem:Bar::bismodifiedto2//wasexpectingittobeanerror有什么想法吗? 最佳答案 这不是修改(或赋值),而是initialization.例如structBar{constintb=5;//initializat

c++ - 为什么可以在构造函数中修改 const 成员?

我很好奇为什么可以在构造函数中修改const成员。初始化中是否有任何标准规则可以覆盖成员的“常量”?structBar{constintb=5;//defaultmemberinitializationBar(intc):b(c){}};Bar*b=newBar(2);//Problem:Bar::bismodifiedto2//wasexpectingittobeanerror有什么想法吗? 最佳答案 这不是修改(或赋值),而是initialization.例如structBar{constintb=5;//initializat

c++ - 为什么要通过 const 引用而不是按值传递?

据我了解:当您按值传递时,该函数会生成传递参数的本地拷贝并使用它;当函数结束时,它会超出范围。当您通过const引用传递时,该函数使用对无法修改的传递参数的引用。但是,我不明白为什么要选择一个而不是另一个,除非在需要修改和返回参数的情况下。如果你有一个没有返回任何内容的void函数,为什么要选择一个而不是另一个?编辑:所以基本上通过const引用传递避免了复制对象。那么在什么情况下复制对象好呢?我的意思是,如果它一直在优化性能,为什么不一直使用const引用呢? 最佳答案 有两个主要考虑因素。一是复制传递的对象的开销,二是当对象是本

c++ - 为什么要通过 const 引用而不是按值传递?

据我了解:当您按值传递时,该函数会生成传递参数的本地拷贝并使用它;当函数结束时,它会超出范围。当您通过const引用传递时,该函数使用对无法修改的传递参数的引用。但是,我不明白为什么要选择一个而不是另一个,除非在需要修改和返回参数的情况下。如果你有一个没有返回任何内容的void函数,为什么要选择一个而不是另一个?编辑:所以基本上通过const引用传递避免了复制对象。那么在什么情况下复制对象好呢?我的意思是,如果它一直在优化性能,为什么不一直使用const引用呢? 最佳答案 有两个主要考虑因素。一是复制传递的对象的开销,二是当对象是本

C++ 纯虚 const 成员函数

如何声明一个也是const的纯虚成员函数?我可以这样吗?virtualvoidprint()=0const;还是这样?virtualconstvoidprint()=0; 最佳答案 来自MicrosoftDocs:Todeclareaconstantmemberfunction,placetheconstkeywordaftertheclosingparenthesisoftheargumentlist.应该是这样的:virtualvoidprint()const=0; 关于C++纯虚c

C++ 纯虚 const 成员函数

如何声明一个也是const的纯虚成员函数?我可以这样吗?virtualvoidprint()=0const;还是这样?virtualconstvoidprint()=0; 最佳答案 来自MicrosoftDocs:Todeclareaconstantmemberfunction,placetheconstkeywordaftertheclosingparenthesisoftheargumentlist.应该是这样的:virtualvoidprint()const=0; 关于C++纯虚c

c++ - 为什么编译器更喜欢 f(const void*) 而不是 f(const std::string &)?

考虑以下代码:#include#include//voidf(constchar*){std::cout我使用g++(Ubuntu6.5.0-1ubuntu1~16.04)6.5.020181026编译了这个程序:$g++-std=c++11strings_1.cpp-Wall$./a.outconstvoid*请注意,注释是为了测试而存在的,否则编译器会使用f(constchar*)。那么,为什么编译器会选择f(constvoid*)而不是f(conststd::string&)? 最佳答案 转换为std::string需要“用户

c++ - 为什么编译器更喜欢 f(const void*) 而不是 f(const std::string &)?

考虑以下代码:#include#include//voidf(constchar*){std::cout我使用g++(Ubuntu6.5.0-1ubuntu1~16.04)6.5.020181026编译了这个程序:$g++-std=c++11strings_1.cpp-Wall$./a.outconstvoid*请注意,注释是为了测试而存在的,否则编译器会使用f(constchar*)。那么,为什么编译器会选择f(constvoid*)而不是f(conststd::string&)? 最佳答案 转换为std::string需要“用户

c++ - 错误 : invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

在我的文件顶部#defineAGE"42"稍后在文件中我多次使用ID,包括一些看起来像的行std::stringname="Obama";std::stringstr="Hello"+name+"youare"+AGE+"yearsold!";str+="Doyoufeel"+AGE+"yearsold?";我得到错误:"error:invalidoperandsoftypes‘constchar[35]’and‘constchar[2]’tobinary‘operator+’"在第3行。我做了一些研究,发现这是因为C++如何处理不同的字符串,并且能够通过将“AGE”更改为“strin

c++ - 错误 : invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

在我的文件顶部#defineAGE"42"稍后在文件中我多次使用ID,包括一些看起来像的行std::stringname="Obama";std::stringstr="Hello"+name+"youare"+AGE+"yearsold!";str+="Doyoufeel"+AGE+"yearsold?";我得到错误:"error:invalidoperandsoftypes‘constchar[35]’and‘constchar[2]’tobinary‘operator+’"在第3行。我做了一些研究,发现这是因为C++如何处理不同的字符串,并且能够通过将“AGE”更改为“strin