tags:

views:

17

answers:

2

I am attempting to use Gtk TextIter objects to lift three-character slices out of a TextBuffer, but am having trouble with the arithmetic. I set up an iterator p to point to the start of the range and want q to point to three characters further on.

I have tried...

q = p + 3;              // Doesn't compile
q = p; q += 3;          // Doesn't compile
q = p; q++; q++; q++;   // Happy

I'd like to know what the correct way to do this is. The third method works, but looks like a ghastly hack.

Any thoughts?

A: 

That's weird.

The underlying C API has gtk_text_iter_forward_chars() which should do the right thing. Maybe read the source, and/or file a bug on the C++ wrapper?

unwind
FWIW, I'm using Gtk 2.20, g++ 4.4.3 and Ubuntu 10.4. I'm glad to hear this isn't quite what you'd expect; I was worried I'd lost the plot entirely.
Brian Hooper
+2  A: 

If you read the documentation you'll see that TextIter doesn't have a + or += operator. It's a bidirectional iterator and not a random access iterator so this is as it should be.

You can use either:

q = p;
std::advance(q, 3);

or

q = p;
q.forward_chars(3);
Staffan
Thank you, Staffan and unwind.
Brian Hooper