L
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.c_str()) > atoi(vecTmp[j].c_str())){
strSwp = vecTmp;
vecTmp = vecTmp[j];
vecTmp[j] = strSwp;
}
}
}
return vecTmp;
}
-- lovecreatesbeauty
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.c_str()) > atoi(vecTmp[j].c_str())){
strSwp = vecTmp;
vecTmp = vecTmp[j];
vecTmp[j] = strSwp;
}
}
}
return vecTmp;
}
-- lovecreatesbeauty