operator overloading question

Is it better to do the following see 1st and second codes snippetts. They both 'do' the same thing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  //oervload stream input operator
std::istream &operator>>( std::istream &input, Point &rhs ) {
    
    float local;
    
    std::cout << "Input x value: ";
    input >> local;
    while ( ! input ) {
        input.clear();
        input.ignore();
        std::cout << "Enter a number: ";
        input >> local;
    }
    rhs.setXValue( local );
    
    std::cout << "Input y value: ";
    input >> local;
    while ( ! input ) {
        input.clear();
        input.ignore();
        std::cout << "Enter a number: ";
        input >> local;
    }
    rhs.setYValue( local );
    
    return input;
}


//oervload stream input operator
std::istream &operator>>( std::istream &input, Point &rhs ) {
    
    //float local;
    
    std::cout << "Input x value: ";
    input >> rhs.xValue;
    while ( ! input ) {
        input.clear();
        input.ignore();
        std::cout << "Enter a number: ";
        input >> rhs.xValue;
    }
    //rhs.setXValue( local );
    
    std::cout << "Input y value: ";
    input >> rhs.yValue;
    while ( ! input ) {
        input.clear();
        input.ignore();
        std::cout << "Enter a number: ";
        input >> rhs.yValue;
    }
    //rhs.setYValue( local );
    
    return input;
}

local does not really do anything for you. Better... either way.. you can argue that it is more readable with local. Not very convincingly, but one could argue it.

I am not a fan of weird operators. By weird, I mean, I would find it odd that


yourclass >> something;

would produce text prompts.

You would expect it to just move data silently.
For this type of function, I would have just called it "getdata" or "putdata" or something instead of trying to operator it.
Topic archived. No new replies allowed.