Ask Your Question

SparesMat::operator++() vs SparesMat::operator++(int)

asked 2016-04-22 19:31:56 -0500

Ralph058 gravatar image

Both SparesMat::operator++() and SparesMat::operator++(int) have the same definition in the docs, namely "moves iterator to the next element". Since it is not defined as SparesMat::operator++(int step) (or some other variable), I am a little confused. Does it increment by the value placed in the parenthesises or is a "don't care"?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-23 01:37:24 -0500

berak gravatar image

updated 2016-04-23 01:54:22 -0500

i guess, this is more an ordinary c++ problem, by convention,

  • operator++() denotes the "postfix" increment (x++)
  • operator++(int) denotes the "prefix" increment (++x)

the int arg in the latter is a dummy, you're not supposed to supply anything there (how would you even?)

the difference between them is the order of evaluation and increment, it's the same as with plain numbers:

int x = 3;
cerr << (x++); // prints 3, first eval, then increment
cerr << (x);   // prints 4 ;)
cerr << (++x); // prints 5, first increment, then eval
edit flag offensive delete link more


Thanks. I understood 'prefix' and 'postfix', but I still don't understand the convention shown. Your comment should be helpful to those who dont.

For an iterator declared as 'it', if i wanted to use "prefix" iterator then, I would use "it.operator++(int)" ?

Ralph058 gravatar imageRalph058 ( 2016-04-23 11:51:32 -0500 )edit

++ it ? if you need to de-reference it at he same time, this would be: *(++it)

berak gravatar imageberak ( 2016-04-23 11:55:37 -0500 )edit

Question Tools

1 follower


Asked: 2016-04-22 19:31:56 -0500

Seen: 127 times

Last updated: Apr 23 '16