在C ++中是否可能导致堆栈上的内存泄漏?
假设基类具有非虚拟析构函数,并且在函数体内,对基类的const引用绑定到派生类类型的未命名对象。
class Base {
public:
~Base(); // non-virtual base class destructor
...
};
class Derived: public Base { ... };
void f() {
const Base& bref = Derived(); // What happens to the memory
// occupied by the unnamed Derived object
// when bref goes out of scope?
}
int main() {
f(); // Will this cause memory leak on the stack?
...
};
当bref
超出范围时,Base
类析构函数会清除未命名的Derived
对象使用的内存吗?