Online Book Reader

Home Category

Professional C__ - Marc Gregoire [272]

By Root 1426 0
to the Muffin class:

class Muffin

{

public:

string getDescription() const;

void setDescription(const string& inDesc);

int getSize() const;

void setSize(int inSize);

bool getHasChocolateChips() const;

void setHasChocolateChips(bool inChips);

void output();

protected:

string mDesc;

int mSize;

bool mHasChocolateChips;

};

// Other method implementations omitted for brevity

void Muffin::output()

{

printf("%s, Size is %d, %s\n", getDescription().c_str(), getSize(),

(getHasChocolateChips() ? "has chips" : "no chips"));

}

Code snippet from Muffin\Muffin.cpp

Using such a mechanism is cumbersome, however. To output a Muffin in the middle of another line of text, you’d need to split the line into two calls with a call to Muffin::output() in between, as shown in the following:

printf("The muffin is ");

myMuffin.output();

printf(" -- yummy!\n");

Overloading the << operator lets you output a Muffin just like you output a string — by providing it as an argument to <<. Chapter 18 covers the details of overloading the << and >> operators.

STRING STREAMS


String streams provide a way to use stream semantics with strings. In this way, you can have an in-memory stream that represents textual data. For example, in a GUI application you might want to use streams to build up textual data, but instead of outputting the text to the console or a file, you might want to display the result in a GUI element like a message box or an edit control. Another example could be that you want to pass a string stream around to different functions, while retaining the current read position, so that each function can process the next part of the stream. String streams are also useful for parsing text, because streams have built-in tokenizing functionality.

The ostringstream class is used to write data to a string, while the istringstream class is used to read data from a string. They are both defined in the header file. Because ostringstream and istringstream inherit the same behavior as ostream and istream, working with them is pleasantly similar.

The following program requests words from the user and outputs them to a single ostringstream, separated by the tab character. At the end of the program, the whole stream is turned into a string object using the str() method and is written to the console. Input of tokens can be stopped by entering the token “done” or by closing the input stream with Control+D (Unix) or Control+Z (Windows):

cout << "Enter tokens. Control+D (Unix) or Control+Z (Windows) to end" << endl;

ostringstream outStream;

while (cin) {

string nextToken;

cout << "Next token: ";

cin >> nextToken;

if (nextToken == "done")

break;

outStream << nextToken << "\t";

}

cout << "The end result is: " << outStream.str();

Code snippet from StringStream\StringStream.cpp

Reading data from a string stream is similarly familiar. The following function creates and populates a Muffin object (see earlier example) from a string input stream. The stream data is in a fixed format so that the function can easily turn its values into calls to the Muffin setters:

Muffin createMuffin(istringstream& inStream)

{

Muffin muffin;

// Assume data is properly formatted:

// Description size chips

string description;

int size;

bool hasChips;

// Read all three values. Note that chips is represented

// by the strings "true" and "false"

inStream >> description >> size >> boolalpha >> hasChips;

muffin.setSize(size);

muffin.setDescription(description);

muffin.setHasChocolateChips(hasChips);

return muffin;

}

Code snippet from Muffin\Muffin.cpp

Turning an object into a “flattened” type, like a string, is often called marshalling. Marshalling is useful for saving objects to disk or sending them across a network.

The main advantage of a string stream over a standard C++ string is that, in addition to data, the object knows where the next read or write operation will take place, also called current position. There may also be performance benefits depending on the particular implementation of string streams.

Return Main Page Previous Page Next Page

®Online Book Reader