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
Зареєструйся Зараз
або Підпишись на майбутні тести