问题
I'm using the pdCurses library and am aiming to only really use strings in my C++ console game but the curses mvinstr() function or any insert function requires a non-const char * as a parameter.
- My solution at first to this problem was simply entering in
string.c_str(), but that returns aconst char *which apparently doesn't work with the function. - Next I put
(char *)string.c_str()but this only causes an unhandled exception. - Finally I just tried
char *test = string.c_str()but that's not compatible withconsteither.
What do I do to solve this?
K i just tried const_cast() and i still get an exception thrown and break.... I don't know why PDcurses only takes non-const char pointers.... =(
alright making a char* buffer didn't work when i used this code (time_s is the sting):
size_t length;
char buffer[12];
length=time_s.copy(buffer,5,0);
buffer[length]='\0';
mvinstr(time_loc_y, time_loc_x, buffer);
i even put a stop before mvinstr() and checked the buffer's contents which was "00 /0" EXACTLY WHAT I WANTED.
but i get an access violation point to "xutility"....
回答1:
mvinstr(x,y,str) and others "take characters (or wide characters) from the current or specified position in the window, and return them as a string in str (or wstr)."
The function will actually modify the string, so you cannot safely cast the const away, especially since c_str specifies that you should not modify the returned string.
You need something along the lines of:
const MAX = 100;
char buf[MAX];
mvinnstr(x, y, buf, MAX);
...error checking...
string s = buf;
Note that I avoided mvinstr in favour of mvinnstr to avoid the potential for buffer overflows.
回答2:
How about
char* buffer = &str[0];
int fetched_len = mvinnstr(time_loc_y, time_loc_x, buffer, str.size());
str.resize(fetched_len);
In general, though, you should make a writable buffer instead of removing const from a pointer that has it. e.g.
vector<char> charvec(MAX_LENGTH);
str = string(&charvec[0], mvinnstr(time_loc_y, time_loc_x, &charvec[0], charvec.size());
回答3:
Cautiously - if the code that uses the was-const data tries to modify it, anything can happen.
In short:
const std::string str = "...";
char *caution = const_cast<char *>(str.c_str());
However, given that you are getting unhandled exceptions, you probably need to make a modifiable copy of the constant string before calling mvinstr(). Maybe:
const std::string str = "...";
char *caution = new char[str.length()+1];
str.copy(caution, str.length()+1);
...call to mvinstr()...
delete[] caution;
回答4:
Since mvinstr is actually storing data into the array pointed at by the char*, you can't use a string there. You need to allocate an array of char, pass that to mvinstr, and then transfer the characters to a string if you want.
If you were using a function that could have been declared with a const char * (i.e. it doesn't actually modify the array), then you could use const_cast<> to remove the const.
const_cast<char *>(str.c_str());
But that's not what you're doing here. const_cast might work if you tried it, but it would be by accident, not because it's supposed to work, and a new compiler or library version could break it at any time.
回答5:
Try something like the following, then use buffer wherever you need a char*. As Ben mentioned, you need to be very careful to keep the buffer larger than the string plus null terminator.
const int BUFFER_SIZE = 255;
string str ("Your string");
char buffer[BUFFER_SIZE];
if (str.length() < BUFFER_SIZE)
{
size_t copy_length;
copy_length=str.copy(buffer,str.length(),0);
buffer[copy_length]='\0';
}
回答6:
Removing const can be done with const_cast, and is sometimes necessary for using legacy interfaces that were written in C and don't use const. In such cases, you can do:
char* ptr = const_cast<char*> (str.c_str());
However, from the cplusplus reference page on c_str():
The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.
This means that it is your responsibility to ensure that ptr is not used to modify the string, and you must handle the lifetime of ptr appropriately. I.e., you cannot continue to use ptr after str is out of scope, or after you call any non-const methods on str. If you do that, you go into Undefined Behavior, and will likely crash.
If you are looking for a safe way to interact with a legacy interface that takes char*, you can make your own writable copy:
char* ptr = new char[ str.size() + 1 ];
strcpy(ptr, str.c_str());
ptr[str.size()] = '\0';
// ... use ptr as much as you want...
delete [] ptr;
回答7:
if (string.empty()) foo(const_cast<char*>(string.c_str());
a better but still evil way to remove const is const_cast(const_string); (its better to find via grep/search)
Witch exception did you encountered? Do u just read the char* or do you change the values? if you change, you should redesign your code.
const char* test = string.c_str();
Does not create a deep copy of string, only a pointer to the internal data representation of string.data().
=> (a suggestion) find a C++ book where you can get a more in deep view of c++. Or something like to C++.
来源:https://stackoverflow.com/questions/6024574/how-to-convert-a-const-char-to-simply-char