Is the following code correct?
class A
{
public:
    int* m_a;
    A(int x) { m_a = new int(x);}
    ~A() {delete m_a;}
};

void f(A a)
{
    cout<<a.m_a;
}


int main()
{
    A a(1);
    f(a);
}
Explanation
The destructor is called twice, the first time when the function f() is exited, the second time it is called when the main() function is exited, trying to delete the memory allocated for the m_a variable again.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты