When passing a class by-value, does the caller or callee call the destructor?

后端 未结 4 824
暗喜
暗喜 2021-02-19 21:34

Suppose that I have the following (trimmed down) code:

class P { P(); P(const P&); ~P(); }

void foo(P x) {
  ...
}

         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 22:03

    The destructor is called whenever an object's lifetime ends, which includes

    end of scope, for objects with automatic storage duration and for temporaries whose life was extended by binding to a reference

    So bar which is the owner of copied object will call dtor on the copied object. Cppreference

提交回复
热议问题