Why can't I add pointers?

前端 未结 7 904
终归单人心
终归单人心 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条回答
  •  旧时难觅i
    2020-11-30 01:36

    Pointer addition is forbidden in C++, you can only subtract two pointers.

    The reason for this is that subtracting two pointers gives a logically explainable result - the offset in memory between two pointers. Similarly, you can subtract or add an integral number to/from a pointer, which means "move the pointer up or down". Adding a pointer to a pointer is something which is hard to explain. What would the resulting pointner represent?

    If by any chance you explicitly need a pointer to a place in memory whose address is the sum of some other two addresses, you can cast the two pointers to int, add ints, and cast back to a pointer. Remember though, that this solution needs huge care about the pointer arithmetic and is something you really should never do.

提交回复
热议问题