Topical Information

Another idea to add to the String class.

Program Information

It is currently possible to translate the contents of a String object into data of any of our six primary types (bool, char, short, long, float, and double) and even into other String objects! But, it isn't possible to extract Cstring data from the object. For instance, given the following declaration:

   char str[40];

We could read this string from a stream by code such as:

    in_stream >> setw(40) >> str;

(Using the setw to protect the length of the array.) But we cannot do so with our String class. That is, we cannot do:

    string_obj >> setw(40) >> str;

How could we add this type of translation/extraction to our String class?

Hint: Look at the parallels between input of a short from a stream and input of a short from a String object.

Hint 2: Remember that setw is a streamable version of the member function width. So the above could have been:

    in_stream >> str;

(*shrug* Just thought it might help...)

Don't forget to test your modifications (perhaps with the original test application?).

Thought Provoking Questions

  1. Why wouldn't you just make a translation operator for C-strings without worrying about width protection?

  2. How does this translation operation differ from the substring operation from before?

  3. Would there be need to overload a getline for extracting arbitrarily terminated Cstring's from a String object?

This assignment is (Level 2.5).


Add (Level 1.5) to overload getline yet again to extract an arbitrarily terminated Cstring from a String object.