post increment operator overload

I am trying to do a rotating buffer, but the postfix++
doesn't behave like a normal char *.

here it is cut down to a bare bones example...

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
#include <cstdio>
#include <cstring>

class keymap {

    char * map;
    size_t size;
    size_t count;

    public:
    keymap(char * string){ map = string; size = strlen(string); count=0;};
    char operator*() {return map[count % size];};
    keymap& operator++(int n) {count++; return *this;};
};

char BT[] = "johnpaulgeorgeringo";
int main (void)
{
    unsigned int t =0;

    keymap map(BT);
    for(t=0 ;t<sizeof BT; t++) {
        putchar(*map++);
        }

    puts("\n============");

    char * p = BT;
    for(t=0 ;t<sizeof BT; t++) {
        putchar(*p++);
        }

    puts("\n");

}


results....
here you can see the dereference seems to come after the ++
unlike the char*
1
2
3
4
5
$ ./increment_operator
ohnpaulgeorgeringojo
============
johnpaulgeorgeringo


any ideas why?
It doesn't behave properly because you're giving yours improper semantics. Postfix is supposed to return a copy of the object made before the original was modified. For example,
1
2
3
4
5
int operator++(int){
    int a=*this;
    *this+=1;
    return a;
}

You, instead, are modifying the object and then returning a reference to the modified object, which makes the operator's semantics identical to prefix.

The solution is not to actually make copies of the object, but to use iterators.
helios ,
int a=*this;
these are miss matched datatype.
It will not complie
It's a conceptual example, so it's not supposed to be compilable. Within that context, it's not an illegal conversion because this is of type int *.
O! Ok .....got it ...helios..thanks
thanks helios. that's got it.
I suspected that might be what goes on with built ins.
and like an idiot I didn't read my book properly I Just found it in Stroustrup 11.11

so a bit of extra overhead.
well, if anyone cares, I've made an iterator.
easier to destroy the memory map,
iterator copies are no problem.
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
#include <cstdio>
#include <cstring>

// postfix involves a copy operation

class keymap {

    char * map;
    size_t size;
    size_t count;

    public:
    class iterator;
    keymap(char * string){ map = string; size = strlen(string); count=0;};
};

struct keymap::iterator {
    size_t count;
    char *p;
    size_t size;
    typedef keymap::iterator T;

    iterator() { count = 0; };
    iterator(keymap& k) { count = 0; size=k.size; p = k.map; };
    char operator*() {return p[count%size];};
    T& operator++() {count++; return *this;};
    T operator++(int n) {T x(*this);count++; return x;};
    T& operator+=(int n) {count+=n; return *this;};
};

char BT[] = "johnpaulgeorgeringo";
int main (void)
{
    unsigned int t =0;

    keymap map(BT);
    keymap::iterator pp;
    pp = map;

    for(t=0 ;t<99; t++) {
        putchar(*pp++);
        }

    puts("\n");

}
Huh? What exactly does this iterator do besides wrap around infinitely?

It can't really be called an "iterator" by the C++ definition because it hardly implements any of
the iterator requirements defined by the STL.

bigearsbilly , Is this code working as expected ?
One thing i dont understand frankly is that how is the char of the string get reversed in the above operator++ () overloaded function . please some one clear my doubt.
Thanks in advance .
Topic archived. No new replies allowed.