Click to See Complete Forum and Search --> : 'const string' to 'char *'
bech
February 19th, 2004, 11:36 AM
Hello !!
How can I convert a 'const string' to 'char *' ?
I have the following code:
int validateXMLfile(char *xmlFile_name);
int Processor::InsertOrder( const string file )
{
int error=0;
error = validateXMLfile( file );
if( error == 1 ) {
cout << "\nERROR VALIDATING file: " << file << endl;
return error;
}
else {
cout << "Order VALIDATED against schema OK "<< endl;
}
}
I get the following compiling error:
Processor::InsertOrder (basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> >)':
Processor.cc:586: cannot convert `const string' to `char *' for
argument `1' to `validateXMLfile (char *)'
Thanks!!
Graham
February 19th, 2004, 11:40 AM
Change
int validateXMLfile(char *xmlFile_name);
to
int validateXMLfile(const char *xmlFile_name);
and call it:
error = validateXMLfile( file.c_str() );
removing constness is almost always a bad idea...
bech
February 19th, 2004, 11:43 AM
Thanks for your suggestion.
The problem is that 'validateXMLfile' belongs to a dynamic library that I have to use and I cannot change.
My code links with that library!!
Assmaster
February 19th, 2004, 08:35 PM
If we ignore the possibillity of using a const_cast<> (Don't do it!), you'll have to make a copy of the string and assign that one instead of the original.int validateXMLfile(char *xmlFile_name);
int Processor::InsertOrder(const string file)
{
int error = 0;
char* TempString = new char[file.size() + 1];
strcpy(TempString, file.c_str());
error = validateXMLfile(TempString);
delete[] TempString;
if(error == 1)
{
cout << "\nERROR VALIDATING file: " << file << endl;
return error;
}
else
{
cout << "Order VALIDATED against schema OK "<< endl;
}
}It ain't pretty, but it works.
Codeplug
February 20th, 2004, 12:33 AM
Originally posted by Assmaster
If we ignore the possibillity of using a const_cast<> ...
const_cast<> only works on class objects.
I would use a C-string off the stack - MAX_PATH should be large enough.
gg
filthy_mcnasty
February 20th, 2004, 02:29 AM
I would use a C-string off the stack - MAX_PATH should be large enough.
considering it's apparently a file name you're right but it's still better practice to use the string classes' length member and size it accordingly.
*basically what assmaster has posted, which btw, i dont see why you say it isn't pretty assmaster, as far as i'm concerned it's just fine*
and as a side note, i'd also pass the string by reference since it's const declared but what you gain in memory use by doing so you can lose in speed by dereferencing etc etc, just a matter of preference.
Graham
February 20th, 2004, 03:15 AM
... and then, when that's all working, track down the author of the library function and spend some time explaining the value of specifying "const" whenever possible...
Assmaster
February 20th, 2004, 12:44 PM
Originally posted by Codeplug
const_cast<> only works on class objects.Well... You can cast away the const from the const char* returned by the std::string.c_str() function, but it cannot however be considered safe, so as I said: Don't do it!Originally posted by filthy_mcnasty
*basically what assmaster has posted, which btw, i dont see why you say it isn't pretty assmaster, as far as i'm concerned it's just fine*What I meant was that it's not as pretty as it could be if the library author had used a const char* to pass in the filename. I this case it's the only (safe) solution, and therefore it's as good as it'll get! :)
Axter
February 21st, 2004, 05:50 AM
IMHO, a more efficient and safer method would be to use a vector<char> type to pass to your function.
Example:
vector<char> Data_v(file .begin(), file .end());
validateXMLfile(&Data_v[0]);
By using this method, you don't have to worry about deleting the data, and the variable gets allocated and initialized with the correct values all in one shot.
Paul McKenzie
February 21st, 2004, 08:10 AM
Originally posted by Axter
IMHO, a more efficient and safer method would be to use a vector<char> type to pass to your function.
Example:
vector<char> Data_v(file .begin(), file .end());
validateXMLfile(&Data_v[0]);
By using this method, you don't have to worry about deleting the data, and the variable gets allocated and initialized with the correct values all in one shot. The only problem I see is that the data is not null-terminated. You can either
Data_v.push_back('\0');
before the call to validateXMLfile, or
#include <vector>
#include <algorithm>
std::vector<char> Data_v(file.length() + 1, 0)
std::copy(file.begin(), file.end(), Data_v.begin());
validateXMLfile(&Data_v[0]);
Regards,
Paul McKenzie
treuss
February 23rd, 2004, 08:31 AM
Well... You can cast away the const from the const char* returned by the std::string.c_str() function, but it cannot however be considered safe, so as I said: Don't do it!
Hmm, after all the reason why const_cast is part of the C++ language is that there are many C-libraries out there with bad interfaces (expecting a char * instead of a const char *).
So if you are using const_cast here, you are using it exactly for the reason its creators had in mind.
But if you don't mind the extra time spent on copying the string, don't use const_cast and stay on the safe side.
BTW, your function
int Processor::InsertOrder( const string file )
should most likely better be called
int Processor::InsertOrder( const string& file )
as there is not much sense in passing a string by value if the function treats it as const anyways. Surprised nobody else pointed that out yet.
Codeplug
February 23rd, 2004, 08:55 AM
Originally posted by Codeplug
const_cast<> only works on class objects.Originally posted by filthy_mcnasty
and as a side note, i'd also pass the string by reference...
gg
treuss
February 23rd, 2004, 09:40 AM
Originally posted by Codeplug
const_cast<> only works on class objects.
Says who? I'm not very good at reading the standard, but a quick look through expr.const.cast did not reveal me anything that confirms your statement. Besides, the following compiles without any warning with g++.
#include <string>
#include <stdio.h>
void lousy_c_function( char * s )
{
printf( "%s\n", s );
}
int main()
{
const std::string mystring( "Hello World!" );
lousy_c_function( const_cast<char*>( mystring.c_str() ) );
return 0;
}
Originally posted by filthy_mcnasty
and as a side note, i'd also pass the string by reference...
Missed that...
filthy_mcnasty
February 23rd, 2004, 10:49 AM
as i was alluding to though, when you pass by reference you really just have a programmer simplified pointer and the process of dereferencing it repeatedly can often be more time consuming than simply passing by value. but all that stuff is trivial and dependant on many other things so yeah, i'd still pass by reference.
Codeplug
February 23rd, 2004, 01:55 PM
>> Say's who?
Me! unfoturnately...
My first mistake was to take MSDN literally: The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.
Second mistake was when I used this to test it out:
const int N = 5;
const_cast<int>(N)++;
How do you spell "assume" again?
gg
asivakumar
February 28th, 2004, 06:17 AM
when u want to change a variable ,why u need to keep it as constant ,if u need to change their are ways for removing the constantness for class
as below
The const_cast operator can be used to remove the constantness from a class.
You cannot use the const_cast operator to directly override a constant variable's constant status
A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const qualifiers
if u need to change for data members to change inside a constant member function use mutable
apart from tht their is no option : (
it will lead to undefined behavior ... so be carefull
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.