Is it safe to use the “this” pointer in an initialization list?

前端 未结 3 643
小鲜肉
小鲜肉 2020-12-02 18:38

I have two classes with a parent-child relationship (the Parent class \"has-a\" Child class), and the Child class has a pointer back t

3条回答
  •  悲哀的现实
    2020-12-02 19:05

    Yes. It's safe to use this pointer in initialization-list as long as it's not being used to access uninitialized members or virtual functions, directly or indirectly, as the object is not yet fully constructed. The object child can store the this pointer of Parent for later use!

提交回复
热议问题