What does the pointer 'this+1' refer to in C++?

前端 未结 5 547
旧巷少年郎
旧巷少年郎 2021-02-03 18:51

I was wandering through the code of Sequitur G2P and found a really strange line of code:

public:
    ...
    const Node *childrenEnd() const { return (this+1)-&         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 18:55

    this is simply a pointer which refers to this object. Since it's a pointer, you can apply pointer arithmetic and even array indexing.

    If this object is an element in an array, this+1 would point to the next object in the array.

    If it's not, well it's just going to treat whatever is at that memory the same as this object, which will be undefined behaviour unless it is the same type.

提交回复
热议问题