草庐IT

const-ref

全部标签

c++ - 使用 std::vector< std::shared_ptr<const T>> 是反模式吗?

很长一段时间我都在使用std::vector和std::shared_ptr手牵手。最近开始使用std::shared_ptr每当需要指向const对象的指针时。没关系,因为std::shared_ptr可以转换为std::shared_ptr然后他们共享相同的引用计数器,一切都感觉很自然。但是当我尝试使用std::vector>等结构时我遇到了麻烦。为简化起见,我将表示这两种结构:templateusingSharedPtrVector=std::vector>;templateusingSharedConstPtrVector=std::vector>;问题是虽然SharedPtr

c++ - 函数更改 const 对象

我有接受const引用作为参数的函数。它不应该改变参数,但它会改变(变量“_isVertex”)。如何解决这个问题?代码如下:#include#includeusingnamespacestd;classElement{public:boolisVertex()const{return_isVertex;};private:bool_isVertex=true;};classElementContainer:publicvector{public:voidpush(constElement&t){//hereeverythingisfinecerr 最佳答案

c++ - 函数更改 const 对象

我有接受const引用作为参数的函数。它不应该改变参数,但它会改变(变量“_isVertex”)。如何解决这个问题?代码如下:#include#includeusingnamespacestd;classElement{public:boolisVertex()const{return_isVertex;};private:bool_isVertex=true;};classElementContainer:publicvector{public:voidpush(constElement&t){//hereeverythingisfinecerr 最佳答案

c++ - 什么时候需要使用 std::ref ?

考虑:std::tuplefunc(constA&a){returnstd::make_tuple(0,std::ref(a));}std::ref是编写正确且可移植的代码所必需的吗?(没有它也可以编译)背景:如果我删除std::ref我的代码构建良好,没有任何警告(g++-4.6-Wall),但不能正确运行。如果感兴趣,A的定义:structA{std::arrayvec;typedefinttype_t;templateA&operator=(conststd::pair,std::tuple>&e){for(inti=0;i 最佳答案

c++ - 什么时候需要使用 std::ref ?

考虑:std::tuplefunc(constA&a){returnstd::make_tuple(0,std::ref(a));}std::ref是编写正确且可移植的代码所必需的吗?(没有它也可以编译)背景:如果我删除std::ref我的代码构建良好,没有任何警告(g++-4.6-Wall),但不能正确运行。如果感兴趣,A的定义:structA{std::arrayvec;typedefinttype_t;templateA&operator=(conststd::pair,std::tuple>&e){for(inti=0;i 最佳答案

c++ - 为什么在通过 const 引用传递临时值时调用复制构造函数?

我将一个未命名的临时对象传递给使用constref参数定义的函数。类的复制ctor是私有(private)的,我得到一个编译错误。我不明白为什么在这种情况下调用复制构造函数。classA{public:A(inti){}private:A(constA&){}};voidf(constA&a){}intmain(){f(A(1));//不出所料,当我将main更改为:Aa(1);f(a);它有效。编辑:编译器是gcc4.1.2 最佳答案 表达式A(1)是一个rvalue5.2.3[expr.type.conv]。在使用rvalue表

c++ - 为什么在通过 const 引用传递临时值时调用复制构造函数?

我将一个未命名的临时对象传递给使用constref参数定义的函数。类的复制ctor是私有(private)的,我得到一个编译错误。我不明白为什么在这种情况下调用复制构造函数。classA{public:A(inti){}private:A(constA&){}};voidf(constA&a){}intmain(){f(A(1));//不出所料,当我将main更改为:Aa(1);f(a);它有效。编辑:编译器是gcc4.1.2 最佳答案 表达式A(1)是一个rvalue5.2.3[expr.type.conv]。在使用rvalue表

c++ - 将 const char* 转换为 wstring

我正在开发基于锌的闪存应用程序的native扩展,我需要将constchar*转换为wstring。这是我的代码:mdmVariant_t*appendHexDataToFile(constzinc4CallInfo_t*pCallInfo,intparamCount,mdmVariant_t**params){if(paramCount>=2){constchar*file=mdmVariantGetString(params[0]);constchar*data=mdmVariantGetString(params[1]);returnmdmVariantNewInt(native

c++ - 将 const char* 转换为 wstring

我正在开发基于锌的闪存应用程序的native扩展,我需要将constchar*转换为wstring。这是我的代码:mdmVariant_t*appendHexDataToFile(constzinc4CallInfo_t*pCallInfo,intparamCount,mdmVariant_t**params){if(paramCount>=2){constchar*file=mdmVariantGetString(params[0]);constchar*data=mdmVariantGetString(params[1]);returnmdmVariantNewInt(native

javascript - 在 javascript 中,我应该尽可能使用 const 而不是 var 吗?

如果创建对对象的引用,并且引用不会改变(即使对象会改变),使用const代替var会更好吗?例如:constmoment=require('moment')exports.getQuotation=function(value){constquotation={};quotation.value=value;quotation.expiryDate=moment().add(7,'days');//Dosomeotherstuffwithquotationperhapsreturnquotation;}; 最佳答案 你可以使用con