samedi 11 mars 2017

C++ Operator>> Overloading with Mutator?

I am trying to figure out a way (if possible) to overload the >> operator to function correctly with a mutator. For example, I want to write:

myClass obj;
cin >> obj.setValue;

and have it correctly set the obj's private value using the available mutator in a single line. I know I can use a temporary variable to store the cin and set the obj value with that, but I'm wondering if there is a more concise method such as this. I'm a novice programmer so this is my futile attempt within myClass:

friend std::istream &operator>>(std::istream &in, void (myClass::*pt)(double newValue)) {
    double temp;
    in >> temp;
    pt(temp);
    return in;
}

I know I can simply overload the myClass as a whole to accept:

cin >> myClass;

but this would prevent me from setting different individual values within myClass at different times.

Thanks

Aucun commentaire:

Enregistrer un commentaire