1

I am trying to provide a c++ interface for a c library I just finished, and I want it to be possible to write

for (DBITable table = db.tables() ; table != NULL ; table++)

where db is a class with a tables() method that returns the DBITable associated with it.

On compilation I get the following error with clang++

error: cannot increment value of type 'DBITable'
for (DBITable table = db.tables() ; table != NULL ; table++)
                                                    ~~~~~^

This is how i have implemented the ++ operator overload method

DBITable
DBITable::operator++()
{
    return next();
}

and it's declared in the DBITable class as

public:
    DBITable operator++();

the table != NULL part worked as I expected by doing this

bool operator!=(void *) 
{
    // evaluate and get the value
    return value;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • The chart at the top of this page should be helpful: http://en.cppreference.com/w/cpp/language/operator_incdec – chris Feb 18 '15 at 20:02
  • 2
    Why would you want such an operator for a table class? Operator overloading is supposed to make code easier to read. What does it mean to "increment" or "advance" a table? And will your code readers be familiar with this interpretation? – Christian Hackl Feb 18 '15 at 20:14
  • @ChristianHackl I will change the class name later. It is internally a linked list. – Iharob Al Asimi Feb 18 '15 at 20:15
  • you may want to use pre-increment to avoid unnecessarily copying the iterator. – ryanpattison Feb 18 '15 at 20:17
  • 2
    @iharob: That has the same problem. It is easy to picture a list iterator being advanced, but certainly not the list itself. As a matter of fact, `std::list` does not have a `++` operator, but its iterators do. – Christian Hackl Feb 18 '15 at 20:17
  • @ChristianHackl yes, immediately after fixing this problem, I am learning how to write an iterator. If you can give any help, a link to a good resource it will be appreciated. – Iharob Al Asimi Feb 18 '15 at 20:18
  • @iharob: [How to implement an STL-style iterator and avoid common pitfalls?](http://stackoverflow.com/q/8054273/845092) – Mooing Duck Feb 18 '15 at 20:21
  • @iharob: While learning how to write iterators is certainly a good thing to do, all I wanted to point out here is that you might use operator overloading for the wrong purpose. – Christian Hackl Feb 18 '15 at 20:21

1 Answers1

15

operator++() is the prefix increment operator. Implement the postfix operator as operator++(int).

The canonical implementations are have the prefix operator return a reference and the postfix operator return by value. Also, you would typically implement the postfix operator in terms of the prefix operator in the interests of least surprise and easy maintenance. Example:

struct T
{
 T& operator++()
 {
  this->increment();
  return *this;
 }

 T operator++(int)
 {
   T ret = *this;
   this->operator++();
   return ret;
 }
};

(Increment/decrement operators at cppreference.)

Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • 1
    Very very simple... :) And of course, there had to be a way to diferentiate the two. I just didn't find anything, the link posted by @chris explains it very well. – Iharob Al Asimi Feb 18 '15 at 20:04
  • 2
    @iharob: You are not to blame for getting this wrong. In my opinion, it is the worst design decision in all of the vast C++ language. – TonyK Feb 18 '15 at 20:10
  • 3
    @TonyK agree I would prefer something like `Class::++operator()` and `Class::operator++()` it would be more obvious. – Iharob Al Asimi Feb 18 '15 at 20:12
  • 1
    @iharob: Or have the `operator++(int)` be compiler generated when you implement `operator++`. – Mooing Duck Feb 18 '15 at 20:23