我可以从非指针变量中删除常量 [英] Can I remove constant from non-pointer variables

查看:98
本文介绍了我可以从非指针变量中删除常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家,


const_cast只适用于指针或引用吗?如果我有一个

常量对象,那么如何从中删除常量属性?

#include< vector>

#include< ; string>

使用命名空间std;


类类型{

};


int main(int argc,char * argv []){


const类型* cptr = 0;

类型* ptr = const_cast<类型* >(CPTR); //#1 ok


const类型ct = Type();

类型t = const_cast<类型>(ct); //#2错误


const vector< Type> cv;

vector< Type> v = const_cast< vector< Type> >(CV); // 3#错误

}


假设我有如下函数,我该如何删除常量

来自参数类型为

const vector< string>&序列,或

const vector< string>顺序?


你能否就这个

函数的声明给我提出更多建议(甚至我所有的代码,谢谢)。我更好地将其声明更改为其他格式吗?例如,我可以声明它

以下之一:


vector< string> &安培;重新排列(向量< string>& sequence);

vector< string>重新排列(向量< string>序列);

vector< string> &安培;重新排列(const vector< string>& sequence);

vector< string>重新排列(const vector< string> sequence);


vector< string>重新排列(常量矢量< string>& sequence){

vector< string> vecTmp = sequence;


// vector< string> vecTmp =

// const_cast< vector< string> >(序列); //#4错误

int iSeqWth = vecTmp.size();

string strSwp;


for(int i = 0; i< iSeqWth; ++ i){

for(int j = i + 1; j< iSeqWth; ++ j){

if(atoi) (vecTmp [i] .c_str())> atoi(vecTmp [j] .c_str())){

strSwp = vecTmp [i];

vecTmp [ i] = vecTmp [j];

vecTmp [j] = strSwp;

}

}

}


返回vecTmp;

}


- lovecreatesbeauty

Hello experts,

Is const_cast only applied to pointers or references? If I have a
constant object, then how can I remove constant attribute from it?
#include <vector>
#include <string>
using namespace std;

class Type{
};

int main(int argc, char * argv[]){

const Type * cptr = 0;
Type * ptr = const_cast<Type *>(cptr); // #1 ok

const Type ct = Type();
Type t = const_cast<Type>(ct); // #2 error

const vector<Type> cv;
vector<Type> v = const_cast<vector<Type> >(cv); // 3# error
}


And suppose I have a function as follows, how can I remove the constant
from the parameter with type of
const vector<string>& sequence, or
const vector<string> sequence?

Could you please give me more suggestions on the declaration of this
function (or even all my code, thanks). Is it better for me to change
its declaration to some other formats? For example, may I declare it
one of the following:

vector<string> & rearrange(vector<string>& sequence);
vector<string> rearrange(vector<string> sequence);
vector<string> & rearrange(const vector<string>& sequence);
vector<string> rearrange(const vector<string> sequence);

vector<string> rearrange(const vector<string>& sequence){
vector<string> vecTmp = sequence;

// vector<string> vecTmp =
// const_cast<vector<string> >(sequence); // #4 error
int iSeqWth = vecTmp.size();
string strSwp;

for (int i = 0; i < iSeqWth; ++i){
for (int j = i + 1; j < iSeqWth; ++j){
if (atoi(vecTmp[i].c_str()) > atoi(vecTmp[j].c_str())){
strSwp = vecTmp[i];
vecTmp[i] = vecTmp[j];
vecTmp[j] = strSwp;
}
}
}

return vecTmp;
}

-- lovecreatesbeauty

推荐答案




你真的不应该试图删除常量。

事情被宣布为不变,因为它们不应被修改。


const_cast运算符不能用于直接覆盖

变量的常量状态。是的,它只适用于指针。


还应该注意,使用结果指针(来自

const_cast运算符)执行写操作可能导致

未定义的行为。


关于重排/排序功能:

参数可以通过引用或值传递。

通过引用传递的参数,为您提供原始

值的引用,更改参数将导致更改原始的

值。

一个按值传递的参数,给你一份原件和

参数的变化不会对原件产生影响(鉴于

拷贝构造函数和赋值运算符,如果正确实现的话)。


如果你打算对原始向量进行排序,请将函数声明为

void rearrange(vector< ; string>& p_vec)

{

//在这里排序登录

//没有退货声明需要

}

如果您打算对原始矢量的副本进行排序,您可以

将该函数声明为:

vector< string>重新排列(向量< string> p_vec)

{

//在这里排序登录

返回p_vec;

}


或者可以做你已经做过的事情

vector< string>重新排列(常量矢量< string> p_vec)

{

//创建p_vec的副本

//对副本进行排序

//返回副本

}


Joe

Hi

You really shouldn''t try to "remove the constant".
Things are declared constant, because they shouldn''t be modified.

The const_cast operator cannot be used to directly override a
variable''s constant status. Yes, it only works with pointers.

It should also be noted, that using the resulting pointer (from the
const_cast operator) to perform write operations might result in
undefined behavior.

About your rearrange/sorting function:
Parameters can be passed by reference or by value.
A parameter passed by reference, gives you a reference to the original
value and changing the parameter would result in changing the original
value.
A parameter passed by value, gives you a copy of the original and
changes on the parameter wouldn''t effect the original (given that the
copy constructor and assignment operator where implemented correctly).

If you intend to sort the original vector, declare the function as
void rearrange(vector<string>& p_vec)
{
// sorting login in here
// no return statement needed
}
Should you intend to sort a copy of the original vector, you could
declare the function as:
vector<string> rearrange(vector<string> p_vec)
{
// sorting login in here
return p_vec;
}

Alternatively on can do what you''ve already done
vector<string> rearrange(const vector<string> p_vec)
{
// create a copy of p_vec
// sort the copy
// return the copy
}

Joe


谢谢。但是在我的代码中有一个从const到非const的转换

没有符合C ++标准的显式转换。它是:


vector< string>重新排列(常量矢量< string>& sequence){

vector< string> vecTmp = sequence; //

非标准转换!所以我想强制执行标准的明确


//转换如下,但失败了。


// vector< string> vecTmp =

// const_cast< vector< string> >(序列); //#4错误。


// ...


}

Thanks. But there is a conversion from const to non-const in my code
without the C++ standard compliant explicit conversion. It is:

vector<string> rearrange(const vector<string>& sequence){
vector<string> vecTmp = sequence; //
non-standard conversion! so I want to enforce the standard explicit

// conversion on it as following, but failed.

// vector<string> vecTmp =
// const_cast<vector<string> >(sequence); // #4 error.

// ...

}


lovecreatesbeauty写道:
lovecreatesbeauty wrote:
你好专家,

const_cast只适用于指针或引用吗?如果我有一个
常量对象,那么如何从中删除常量属性?
Hello experts,

Is const_cast only applied to pointers or references? If I have a
constant object, then how can I remove constant attribute from it?



为什么你想要它?


const是有原因的!


Ian


Why on earth would you want to?

const is there for a reason!

Ian


这篇关于我可以从非指针变量中删除常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆