mardi 22 octobre 2019

calling class method through shared ptr passes(without initialization) but accessing field causes segv(which is expected)

have a doubt regarding method calls from a class variable that points to a nullptr, I expected it to cause segv but it passed and SEGV was observed only when i tried accessing a class variable through th null ptr object. Wanted to know how does it gets to a function if class variable basically points to null which should have failed

#include <bits/stdc++.h>
using namespace std;

class A {

public:
 void func() {cout << "here";}
 int x;
};

int main()
{
    shared_ptr<A> a;
    a->func(); // passes without any error even though a is nullptr 
    //cout << a->x; this causes SEGV expected
    return 0;
}

My expectation was that it should have failed in a->func() but it passed that, how does it work ?

Aucun commentaire:

Enregistrer un commentaire