mercredi 27 mai 2015

Operator bitwise left shift

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.

Aucun commentaire:

Enregistrer un commentaire