This one will explain you more than if I use plain words. Try compiling it with any compiler you want :) But note, it's UB according to standards!
#include
using namespace std;
class Armor
{
public:
void set(int data)
{
cout << "set("<data = data; //dereference it here
}
void get()
{
if(this) cout << "data = " << data << "\n";
else cout << "Trying to dereference null pointer detected!\n";
}
int data;
};
int main()
{
cout << "Hello World" << endl;
Armor a;
a.set(100);
a.get();
Armor* ptr1 = &a;
Armor* ptr2 = 0;
ptr1->set(111);
ptr2->set(222);
ptr1->get();
ptr2->get();
return 0;
}
Then read about __thiscall - and all comments above.
Hello World
set(100)
data = 100
set(111)
set(222)
I am called on NULL object! I prefer to not crash!
data = 111
Trying to dereference null pointer detected!