Can a C++ class determine whether it's on the stack or heap?

后端 未结 15 2184
有刺的猬
有刺的猬 2020-12-13 04:14

I have

class Foo {
....
}

Is there a way for Foo to be able to separate out:

function blah() {
  Foo foo; // on the stack
         


        
15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 04:19

    I would recommend using smart pointers instead. By design, the class should have data and information about class. Book-keeping tasks should be delegated outside the class.

    overloading new and delete can lead to more holes than you can imagine.

提交回复
热议问题