Quote:Original post by SiCrane
Quote:Original post by paulecoyote
*sigh* Code:
All that code proves is that you don't understand the term "copy constructor". A copy constructor is a constructor for a class X that takes as an argument a cv qualified reference to X. That's it, no other constructor for a class X is a copy constructor.
Devja already posted the correct way to create a std::string from a non-null terminated pointer to a char array. My statement was directed exclusively at your post about the strcpy() function, which still stands.
changed the one line to use the copy constructor, rather then just the constructor. Code still has same output.
const char* testString=("abcd\x00 efghijk"); cout << "testString: " << testString << endl; std::string nullTest; // Just for SiCrane nullTest = (testString); if (4==nullTest.length()) { cout << "Well looks like only the first bit of the string was copied across, just like I was trying to say all along: " << nullTest << endl; } else { cout << "All the string was copied, my bad." << endl; } std::string nullTest2; nullTest2.resize(13); nullTest2.assign( testString, 13); if ((13==nullTest2.length()) && ('k'==nullTest2[12])) { cout << "Well looks like the whole string was copied this time, because you told it what size it should be and used assign to tell it how many characters to copy: " << nullTest2 << endl; } else { cout << "Not all the string was copied, my bad." << endl; }
Quote:Original post by paulecoyote but then you do actually have to tell it what size it should be.
... which Deyja followed up with a concrete example (telling it how many characters where in the source string, IE
tell it what size it should be:
Quote:Original post by Deyjastd::string(string_with_nulls,length)
Quote:Original post by kristoferos Thanks for all of your replies, passing the length as an argument worked
So we are agreed that the correct way is to tell pass an stl string how many characters it is expecting for a string with nulls embedded, right - though whatever method (construction, resizing, whatever).
Quote:Original post by SiCranestd::string has a perfectly usable copy constructor and assignment operator.
But I stand by what I was saying in that passing a c-style string to an stl string copy contructor with null terminating characters in won't work, as that code fragment was trying to illustrate.
As for the strcpy stuff I meant processing
before it's handed over to the stl string, as you say it isn't very advisable to use that stuff on a stl string. Because in context of the original posters problem, he seems to be reading things in from somewhere else then putting them in the string.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.