3

What is the recommended approach for updating an object after creation with a stream of data? I would like to avoid using a number of SetXX methods.

Say I have a class that looks like this;

class Model { public: Model(int, double, std::string); private: int a; double b; std::string c; }; 

One approach to solving this was adding operator;

friend Model& operator<<(Model&, std::stringstream&) 

The usage of the above code;

// create model Model model(...); // do stuff // update model later model << stream; 

This approach compile and runs.

Just wondering if this is a good approach and if it has any flaws \ limitations? Notice that most example online using operator<< use it differently than what I am doing above.

5
  • 4
    The usual way to get information from a stream is the input (or right shift) operator >>, so you do e.g. stream >> model, the way you get input from any other stream like std::cin. Your way works, but it's unusual and will not be easy to understand or comprehend by others except yourself (and maybe not even yourself in a few months time). Can you please explain why you want it the opposite way of what it's normally done? What's the rationale? Commented May 27, 2015 at 7:38
  • 1
    If this is working code then the question is probably better suited to codereview.stackexchange.com Commented May 27, 2015 at 7:39
  • 2
    Definitely a good question, however it might be more suitable for codereview, as it deals with working code which does not show an error. Commented May 27, 2015 at 7:40
  • Joachim: No real rationale, just did what felt natural. Your advice look good. Thank you. Commented May 27, 2015 at 8:50
  • Paul: Will do that next time. Did not know about that site. Commented May 27, 2015 at 8:51

2 Answers 2

2

I would suggest to follow the same notation as in the standard library: use operator>> for input and return reference to the stream, not the Model. This way it will be more readable for the others (who are familiar with the standard library but not your notation), and it will allow for chained inputs:

friend std::istream & operator>>(std::istream & s, Model & m) { m.data = ... return s; } Model m1, m2; std::cin >> m1 >> m2; 

As std::istringstream is derived from std::istream, this operator will work for it as well as all other input stream types.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd consider writing an update method that takes a stream instead of using the operator. The flaw with using the operator << is, as you stated, that it's not usually used for that purpose, which will probably irritate everyone looking at your code that doesn't know how you implemented the operator. stream >> model is more commonly used, as stated in the comments.

3 Comments

I have no issue with setXX methods. In this context I wanted one method that did the updating and could determine whether to trigger a signal that other subscribe.
If I changed to using operator>>, would you still prefer using an update method? What is the rationale for that?
@Lars I would still prefer the update method. In my opinion update states more clearly what is done, rather than the more generic >>. This might be a personal preference (I have a Java background, which doesn't support operator overloading), though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.