Destructor called when objects are passed by value

后端 未结 3 940
执笔经年
执笔经年 2021-02-14 06:00

Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the cal

3条回答
  •  无人共我
    2021-02-14 06:40

    Your object was copied field by field, so inside SomeFunc you have two instances of Sample - s1 and x (but only x is accessible), and value of x.ptr is equal to s1.ptr. Then when SomeFunc ends, destructor is called and from that point s1.ptr points to unallocated memory. Its called "dangling pointer".

提交回复
热议问题