I think your original idea in your first post... std::stringstream was probably the best way. You just needed to add a istream operator >> to handle the CComponent. Something like this:
class CComponent
{
private:
std::string something;
public:
CComponent() noexcept = default;
friend std::istream& operator >> (std::istream& in, CComponent& component);
};
istream& operator >> (istream& in, CComponent& c)
{
in >> component.something;
return in;
}
Which could then be used like this:
const std::string test_haakon = "Lorem ipsum dolor sit amet";
CComponent test_component;
std::stringstream test_string_stream(test_haakon.c_str());
test_string_stream >> test_component;
Merry Christmas to you and your family. Best Wishes, -David Delaune UPDATE: I really hate answering these modern C++ questions because it takes a lot more effort. I shouldn't have said that it was 'probably the best way' because if you use the code above it will make a copy of the data three times. If you are able to use C++17 in your project then you will be able to avoid one of those copies by deriving a class from std::basic_streambuf as such:
class CComponent
{
private:
std::string something;
public:
CComponent() noexcept = default;
friend std::istream& operator >> (std::istream& in, CComponent& component);
};
template >
class make_stream_buffer_no_copy : public std::basic_streambuf
{
public:
make_stream_buffer_no_copy(CHAR* p, size_t len)
{
basic_streambuf::setg(p, p, p + len);
basic_streambuf::setp(p, p + len);
}
std::basic\_stringstream get\_stream()
{
return std::basic\_stringstream(basic\_streambuf::pbase(), ios\_base::in | ios\_base::out);
}
};
istream& operator >> (istream& in, CComponent& c)
{
in >> c.something;
return in;
}
Which you would use like this:
CHAR p2[] = "Lorem ipsum dolor sit amet";
CComponent test_component2;
make_stream_buffer_no_copy no_copy(p2,strlen(p2));
no_copy.get_stream() >> test_component2;
You can avoid ALL copying if you are willing to extend your CComponent class.