2

I wonder if there is any possibility to create function returning some part of ostream, like in example:

#include <iostream> class Point { public: Point(int x, int y){ this->x = x; this->y = y; } ?? getXY(){ // I wish this function returned ostream return ??; } private: int x,y; }; int main() { Point P(12,7); std::cout << "(x,y) = " << P.getXY(); // (12, 7); } 

I wish the output was:

(x,y) = (12,7) 

I don't want getXY() to return any string or char array. May I somehow return part of stream?

3
  • Why not just return a std::string? Commented Oct 21, 2011 at 20:54
  • It's quite obvious I can do it this way, but it's part of my "homework" from OOP at my University. I cannot edit line: std::cout << P.getXY(); so it has to be returned by the function. And I want to know if I can do it in other way than using string. Commented Oct 21, 2011 at 21:08
  • You cannot return std::ostream, it has a private copy constructor. Similarly for std::stringbuf. What you can do though is return an std::pair with the two values and overload operator<< for the pair to print what you want. Commented Oct 21, 2011 at 21:36

4 Answers 4

5

Generally this is done by overloading the stream insertion operator for your class, like this:

class Point { public: Point(int x, int y){ this->x = x; this->y = y; } int getX() const {return x;} int getY() const {return y;} private: int x,y; }; std::ostream& operator<<(std::ostream& out, const Point& p) { out << "(x,y) =" << p.getX() << "," << p.getY(); return out; } 

Used as:

Point p; cout << p; 
Sign up to request clarification or add additional context in comments.

Comments

2

Why not just implement operator << for your class? It would do exactly what you want.

2 Comments

That's not the problem. I could overload << operator but in the end it would have always the same result. For example, I want to have in class two functions I could use in stream. It's like I want to overload << but just for function.
Then just implement a bunch of friend classes that would only implement the I/O, each with its own operator <<.
2

If you only need to print one sort of output, just override operator<< in your containing class. But, if you need to print different sorts of output according in different contexts, you might try creating objects of different proxy classes.

The proxy object could hold a reference to Point, and print it (or portions of it) according to your needs.

I would make the proxy objects private member classes of Point to restrict their visibility.

EDIT Removed sample -- I didn't notice this was homework.

1 Comment

+1, with the given constraints the proxy-object method is the best way to go.
1

In addition to your Point code, you can use a helper function (below, display()) as an alternative to overloading:

std::ostream& display(std::ostream &os,Point &p) const { os<< p.x << p.y ; return os; } int main() { Point p; display(std::cout,p); // This will call the display function and // display the values of x and y on screen. } //main 

The display function can be made a friend of class Point if it needs to access private members.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.