sylcheung@gmail.com wrote:[color=blue]
> I have a class called 'Rect' and it has a method like this:
>
> string Rect::toString() {
> ostringstream ost;
> ost << "x:" << x;
> ost << " y:" << y;
> ost << " w:" << w;
> ost << " h:" << h;
>
> return ost.str();
> }
>
> And in my program, I call toString() like this:
> Rect r;
> cout << r.toString();[/color]
Others have answered your memory questions, but I thought I'd point out
something, since you said you're new to STL.
Usually, the "C++ way" of doing this is to define operator<< for your
class, so that you can output the data to any ostream:
#include <iostream>
class Rect {
public:
friend std::ostream& operator<<(std::ostream& o, const Rect& r);
Rect() : x(0), y(0), w(0), h(0) { }
private:
int x;
int y;
int w;
int h;
};
std::ostream& operator<<(std::ostream& o, const Rect& r)
{
return o << "x:" << r.x
<< " y:" << r.y
<< " w:" << r.w
<< " h:" << r.h;
}
int main()
{
Rect r;
std::cout << r << '\n';
return 0;
}
--
Marcus Kwok