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

前端 未结 5 549
旧巷少年郎
旧巷少年郎 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 19:17

    As it is NLP it makes sense to optimize memory management. I assume you find overloaded new/delete methods as well.

    The this+1 construct assumes all objects reside in an array. The name 'childrenEnd' of the method indicates it returns a pointer to an address of the end of the children of the current node.

    Thus you are looking at an implementation of a tree structure. All siblings are adjacent and their children as well.

提交回复
热议问题