-2

I used following code to provide a global variable.

namespace STR.Pref
{
    public static class Pref
    {
        public static Lang PrimaryLang { get; set; } = Lang.Sinhala;
        public static bool InsTrans { get; set; } = true;
        public static HotKey Key { get; set; } = new HotKey();
    }
}

So I can use following code to assign value to that global variable (any file).(without instantiating)

    private static void SetValue(Pref_tempObj tempObj)
    {
        Pref.Pref.Key = tempObj.Key;
        Pref.Pref.InsTrans = tempObj.InsTrans;
        Pref.Pref.PrimaryLang = tempObj.PrimaryLang;
   }

The program works perfectly,But recently I was interested in c++ so I made a decision to write that program in c++/clr with the small update. I'm bit new to c++ and I don't understand how can I accomplish this using c++.(whether using pointer or something else)

getek mer
  • 113
  • 2
  • 5

1 Answers1

1

Ok, I really not recommend that but you can do following this instructions.

On a Global.h file (I mean a header will be include on every .cpp)

extern MyType MyTypeVar;

Declare on a .cpp

MyType MyTypeVar

And on every .cpp that you will include the Global.h have access to MyTypeVar.

See:

When to use extern in C++

Extern

What is the function of extern

Kevin Kouketsu
  • 786
  • 6
  • 20
  • But linker error: MyTypeVar already defined (and I already tried) – getek mer Nov 23 '18 at 18:46
  • Have you declared on a .cpp with the .h correctly? See this link: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c here have a better code sample – Kevin Kouketsu Nov 23 '18 at 18:50