Next: , Previous: Introduction, Up: Top


2 The autosprintf class

An instance of class autosprintf just contains a string with the formatted output result. Such an instance is usually allocated as an automatic storage variable, i.e. on the stack, not with new on the heap.

The constructor autosprintf (const char *format, ...) takes a format string and additional arguments, like the C function printf.

Conversions to char * and std::string are defined that return the encapsulated string. The conversion to char * returns a freshly allocated copy of the encapsulated string; it needs to be freed using delete[]. The conversion to std::string returns a copy of the encapsulated string, with automatic memory management.

The destructor ~autosprintf () destroys the encapsulated string.

An operator << is provided that outputs the encapsulated string to the given ostream.