because I declared alot of my char*s like this:
char* variable = "stuff";
this way brings up warnings.
because I declared alot of my char*s like this:
char* variable = "stuff";
this way brings up warnings.
Time to change to use std::string then.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
but alot of functions I use only accept char*.
How would you declare a char* normally?
What functions might these be? There are generally std::string equivalents for standard library functions that deal with null terminated strings.Originally Posted by thegr8n8
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am using this networking engine.
Ah, now that changes things.Originally Posted by thegr8n8
It depends on the situation. One option is to use a std::vector<char> that is of the desired size (and is not empty), then pass &v[0] as an argument, where v is the vector name. This would not be appropriate for your original example in this thread (where std::string should be used), but it would be appropriate if you are trying to allow the engine to populate some dynamically sized buffer.
You can actually do the same trick with a std::string, except that due to a defect in the current C++ standard it is not guaranteed to work in the same way as for a vector.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If you're going to call a function that uses char * do this:
You can use strings the rest of the time. And there are a number of fancy containers that will probably make things "easier".Code:size_t tempsize = size_t(str.size()); char *temp = new char[tempsize + 1UL]; str.copy (temp, tempsize); temp[tempsize] = '\0'; foo (temp, tempsize); delete[] temp;
Note that if foo might throw an exception then whiteflags' example is not exception safe.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
I am now confused
Yes. Actually, adeyblue once pointed out to me that this should not be a problem because std::basic_string's operator[pos] is defined as doing something equivalent to returning data()[pos]. Unfortunately, data() is itself declared as returning a const char*, and it is explicitly forbidden to modify the corresponding character array. Hence, if one accepts this definition at face value, it means that one cannot actually modify a character returned by the non-const operator[], which does not make sense.Originally Posted by pianorain
In the next version of the C++ standard, this problem will be fixed by defining operator[] using iterators instead, and also std::string will be required to be implemented with a single contiguous block of memory.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
this :
what is teh size_t?Code:size_t tempsize = size_t(str.size()); char *temp = new char[tempsize + 1UL]; str.copy (temp, tempsize); temp[tempsize] = '\0'; foo (&temp[0], tempsize); delete[] temp;