0

So I'm in a basic programming II class. We have to create a program that makes 4 different functions that will change the way an operator works. I've looked up multiple examples and sets of text that display how to do this, but I cannot make which way of what any of the code means. To me something like this should work.

int operator++()
{
    variableA--;
}

To me, this says if you encounter a ++, then -- from the variable, now obvious it doesn't work like this. All the examples I've found create their own data type. Is there a way to overload an operator using an int or a double?

3
  • Operator overloading is a lot more involved than one's initial intuition might suggest. Your teacher should have gone over it before assigning you an exercise on it, as it seems pretty involved at first, and there are many special cases. Surely your book has a section on it. There's no easy way of getting around just sitting down and reading about it. Commented Mar 9, 2011 at 3:51
  • He never went over it and we don't even have books. So that is why I'm coming here to try and find help. Commented Mar 9, 2011 at 3:58
  • Bummer. There's an entire chapter on them in this free book: Thinking in C++, chapter 12. Worth a read, though the book overall is only so-so. Commented Mar 9, 2011 at 4:02

3 Answers 3

6

All the examples create their own data type since this is one of the rules for operator overloading: An overloaded operator must work on at least one user-defined type.

Even if you could overload ++ for integers, the compiler wouldn't know which one to use -- your version or the regular version; it would be ambiguous.

You seem to think of operators as single functions, but each overload is a completely separate function differentiated by its function signature (type and sometimes number of arguments), while having the same operator symbol (this is the definition of "overloading").

So, you can't overload ++ to always do something different; this would really be operator overriding, which C++ doesn't allow.

You can define ++ for a type you've created though:

class MyType {
public:
    int value;
};

MyType const& operator++(MyType& m) {   // Prefix
    ++m.value;
    return m;
}

const MyType operator++(MyType& m, int) {   // Postfix (the 'int' is just to differentiate it from the prefix version)
    MyType temp = m;
    ++m.value;
    return temp;
}

int main() {
    MyType m;
    m.value = 0;
    m++;    // Not m.value++
    cout << m.value;    // Prints 1
}

Note that this set of ++ operators was defined outside of the MyType class, but could have been defined inside instead (they would gain access to non-public members that way), though their implementations would be a little different.

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

1 Comment

Most excellent, this is pretty much exactly what I was looking for. I got +, -, and / working I was thinking of doing Mod but that was probably more work than I intended. Much appreciated.
1

You can't overload operators of built-in types. (Well, technically you can overload things like "int + MyClass" - but not when both sides are built-in types)

Comments

0

Take a look at How can I overload the prefix and postfix forms of operators ++ and --?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.