lundi 26 septembre 2016

Initializing static member from member function of null object

I have below code in c++/cli.

#include "stdafx.h"

using namespace System;

ref class Test
{
public:
    void SetI(int k)
    {
        i = k;
    }
private:
    static int i = 0;
};

int main(array<System::String ^> ^args)
{
    Test ^obj = nullptr;
    obj->SetI(10);

    Console::WriteLine(L"Hello World");
    return 0;
}

in main eventhough obj is nullptr and called SetI, it is working without any crash. If varibale i of Test becomes member variable, SetI called on nullptr crashed. What could be the reason.

Aucun commentaire:

Enregistrer un commentaire