Why can't I add pointers?

前端 未结 7 897
终归单人心
终归单人心 2020-11-30 00:38

I have code very similiar to this:

LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator+(const Iterator& right)const
{
    return (this + &am         


        
7条回答
  •  长情又很酷
    2020-11-30 01:29

    I suppose in your example the result you were expecting was to add or subtract a pointer to an offset to be moved, not to another pointer.

    In C++ you can subtract 2 pointers (getting the offset in memory between them) or add a pointer to an integer value (moving a pointer to another memory location being incremented by value * sizeof(object_class)). Just adding 2 pointers do not make sense in C++, but if you are sure you want to add 2 memory locations adresses, just add then as unsigned integer values (using typecast).

提交回复
热议问题