草庐IT

const-ref

全部标签

c++ - 定义中的const值参数而不是声明中的const值参数真的是C++吗?

这类似于(但不同于)thisquestion.下面是一些简单的测试代码,用于说明我在SunCC中发现的一些怪异之处://---------------main.cpp#include"wtc.hpp"intmain(int,char**){testyt;t.lame(99);return0;}//--------------wtc.hpp#ifndefWTC_HPP_INCLUDED#defineWTC_HPP_INCLUDEDclasstesty{public:voidlame(int);};#endif//---------------wtc.cpp#include#include

c++ 在签名中使用 const 的含义

请帮助我理解以下签名:err_typefuncName(constType&buffer)const;那么对于第一个const,这是否意味着Type的内容不能改变或者引用不能改变?其次,第二个const是什么意思?我什至没有任何提示。提前致谢,jbu 最佳答案 第二个const表示可以在const对象上调用该方法。考虑这个例子:classfoo{public:voidconst_method()const;voidnonconst_method();};voiddoit(){constfoof;f.const_method();//

c++ - RAII : Initializing data member in const method

在RAII中,资源在被访问之前不会被初始化。但是,许多访问方法都声明为常量。我需要调用一个mutable(非常量)函数来初始化一个数据成员。示例:从数据库加载structMyClass{intget_value(void)const;private:voidload_from_database(void);//Loadsthedatamemberfromdatabase.intm_value;};intMyClass::get_value(void)const{staticboolvalue_initialized(false);if(!value_initialized){//The

c++ - C++ 中的 const char*

C++中的字符串表达式是如何工作的?考虑:#includeusingnamespacestd;intmain(intargc,char*argv[]){constchar*tmp="hey";delete[]tmp;return0;}“嘿”表达式存储在哪里以及如何存储,为什么在我尝试删除它时出现段错误? 最佳答案 在这种(有些特殊的)情况下,它的存储位置留给编译器来决定。然而,这对您来说并不重要-如果您不使用new分配内存,则尝试使用delete释放它并不是一件好事。您不能删除以您分配的方式分配的内存。如果您想控制该资源的释放,您应

c++ - 在 boost::ptr_unordered_map 中存储指向 const 对象的指针

我似乎做不到boost::ptr_unordered_map工作-底层实现看起来像是将东西转换为void*.我是否只需要硬着头皮让我的方法包装对此的访问做一个const_cast插入项目时,或者我在这里遗漏了什么?有什么方法可以存储指向const对象的指针(constFoo*)? 最佳答案 看起来这是不可能的。解决方法是包装对ptr_unordered_map的访问.插入方法应该采用constauto_ptr然后执行const_cast插入它。如果您在删除元素时将auto_type交还给客户端代码,则需要从中解压指针并将其传输到co

c++ - g++ 不允许在 lambda 中通过引用广义捕获 const 对象?

这被g++(4.9.3和5.2.0)拒绝,但被clang3.5.0接受:intmain(){constintci=0;autolambda=[&cap=ci](){};}g++给出错误:将“constint”绑定(bind)到“int&”类型的引用会丢弃限定符。看起来g++拒绝允许捕获非常量引用,当然除了使用普通的旧C++11捕获[&ci]。这似乎是一个非常奇怪的约束,也许是g++中的错误? 最佳答案 您的代码有效。§5.1.2/11去Aninit-capturebehavesasifitdeclaresandexplicitlyc

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

将非const指针转换为const指针是合法的。那为什么将指向非const的指针转换为指向const的指针是不合法的呢?例如,为什么下面的代码是非法的:char*s1=0;constchar*s2=s1;//OK...char*a[MAX];//akachar**constchar**ps=a;//error! 最佳答案 来自标准:constcharc='c';char*pc;constchar**pcc=&pc;//notallowed*pcc=&c;*pc='C';//wouldallowtomodifyaconstobject

python - 为什么不能使用 std::ref 将对象传递到 Boost.Python 模块中?

环境:使用Python3.5编译的Boost1.61.0以下C++代码输出12:classA{public:intfunc(){return12;}};BOOST_PYTHON_MODULE(bridge){usingnamespaceboost::python;class_("A",no_init).def("func",&A::func);}intmain(){Aa;PyImport_AppendInittab("bridge",PyInit_bridge);Py_Initialize();usingnamespaceboost::python;dictdictMain=extra

c++ - 使用 ref 限定符实现方法

我无法实现以下代码templatestructFoo{std::vectorvec;std::vectorgetVector()&&{//fillvectorifempty//andsomeotherworkreturnstd::move(vec);}std::vectorgetVectorAndMore()&&{//dosomemorework//returngetVector();//notcompilereturnstd::move(*this).getVector();//seemswrongtome}};intmain(){Foofoo;autovec=std::move(f

c++ - 关于将 const 引用绑定(bind)到临时对象的子对象

代码如下#includestructP2d{doublex,y;P2d(doublex,doubley):x(x),y(y){}~P2d(){printf("Destructorcalled\n");}};P2dcenter(){returnP2d(10,10);}intmain(intargc,constchar*argv[]){constdouble&x=center().x;printf("x=%.18g\n",x);return0;}g++(版本5.2.0)将在中输入printf之前销毁P2d临时实例main,但该值无论如何都会被保留(即,不是将x绑定(bind)到临时P2d实