Phuc Hong
Phuc Hong

Reputation: 1

Overload function of operator with enum type data

Use the enum below to implement the next 3 operators

enum day {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};
day *operator++(day &d);
day &operator--(day &d);
day const &operator--(day &d);

This is my code:

day *operator++ (day &d) 
{
    d = (day)(d + 1);
    return &d;
}
day &operator--(day &d)
{
    d = (day)(d - 1);
    return d;
}
day const &operator--(day &d) 
{
    d = (day)(d - 1);
    return d;
}

But I get my a overload function with the 3rd func. How can i fix it?

Upvotes: 0

Views: 305

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149185

As said by @NathanOliver, C++ does not allow programmer to have overloads that differ only by the return type. So your third and last overload cannot coexists with second one.

Anyway, you should not write the first overload either. Common programmers use ++ operator and syntactic sugar for x = x+1 (more or less because of the difference between prefix and postfix versions).

But it is really weird that the operator++ or the addition returns a pointer. It is at least confusing, even if syntactically acceptable by the compiler.

Upvotes: 1

Related Questions