locked
Non C++/CX class. Need acccess to it. How to make it global?

    Question

  • Sorry again. This is maddening! I have looked and looked for resources (books, forums,,..) again and again and I cannot find any (lots of C#, JavaScript but nothing on Native C++). If anyone has any resource suggestions I would really appreciate it.

    So, I have a 'standard' C++ class. I do not want to convert it to a Windows Store (RT, or whatever) class. It compiles. links. I can new it and it works. What I cannot do however is figure out how to create a Global variable to hold an instance of my class.

    Here is on of the many things I have tried:

    In App.xaml.h

    #include "MyClass.hpp"
    class GlobalMyClass *MyClass;

    namespace TestWin8
    {
    .......

    App.xaml.cpp
    void App::OnLaunched(LaunchActivatedEventArgs^ args)
    {class MyClass *StackPointerMyClass=(class MyClass *)NULL;

     // I Have StackPointerMyClass Here Just To Illustrate That
     // MyClass In Fact Functions. This All Works Just Fine.
     StackPointerMyClass=new MyClass();
     (StackPointerMyClass->Initialize)();
     (StackPointerMyClass->ListConnections)();

     // GlobalMyClass Is NULL (0000000 Pointer)
     GlobalMyClass=StackPointerMyClass;
     // GlobalMyClass Is Still NULL After i Assigned It (0000000 Pointer)

     So how do I set up a global instance of MyClass? I understand that I could convert MyClass to a Windows Runtime class (ref new...) and hang it off of App:: but I am attempting to maintain a code base for Windows CE 6.0, Xp Sp3, Vista(x86 and x64), Windows 7(x86 and x64).

    Again, any help would be greatly appreciated.

     
    Tuesday, April 16, 2013 8:34 PM

Answers

  • You can do this the same way you would in any other standard C++ app. Instead of storing the class in a raw global take a look at the Singleton Pattern.

    That said, you can just define your global variable and assign the object to it.

    I'm not quite sure what your sample code is trying to do here. It looks like the class and the objects of the class are getting mixed up. I can't tell if MyClass is the class and GlobalMyClass is a variable or the other way around. They are each used in both ways in different parts of the code snippet.

    Assuming that the class is "MyClass" and the global variable is globalMyClass you could do something like the following:

    class MyClass { .... }
    
    MyClass* globalMyClass;
    
    void App::OnLaunched(...) 
    {
       globalMyClass = new MyClass();
       ...
    }
    
    

    --Rob

    Wednesday, April 17, 2013 3:05 AM
    Owner
  • Thanks Rob.

    I am loosing my mind here. the StackPointerMyClass is there just to illustrate that the MyClass has nothing wrong with it. I was trying to attempt to discourage persons from saying maybe something is wrong with MyClass. Though I could have swore that I did try this, apparently I did not. GlobalMyClass is now accessible anywhere within the application that I include App.xaml.h and am using namespace TestWin8.

    In App.xaml.h

    #include "MyClass.hpp"
     

    namespace TestWin8
    {

     extern class GlobalMyClass *MyClass; // extern. INSIDE THE namespace

    .......

    App.xaml.cpp

    namespace TestWin8{ class GlobalMyClass *MyClass; // Declare it INSIDE the namespace} 

    void App::OnLaunched(LaunchActivatedEventArgs^ args)
    {class MyClass *StackPointerMyClass=(class MyClass *)NULL;

     // I Have StackPointerMyClass Here Just To Illustrate That
     // MyClass In Fact Functions. This All Works Just Fine.
     StackPointerMyClass=new MyClass();
     (StackPointerMyClass->Initialize)();
     (StackPointerMyClass->ListConnections)();

     // GlobalMyClass Is NULL (0000000 Pointer)
     GlobalMyClass=StackPointerMyClass;
     // GlobalMyClass Is Now assigned to StackPointerMyClass

    Thanks.

    • Marked as answer by RSullivan59 Wednesday, April 17, 2013 1:46 PM
    Wednesday, April 17, 2013 1:39 PM

All replies

  • You can do this the same way you would in any other standard C++ app. Instead of storing the class in a raw global take a look at the Singleton Pattern.

    That said, you can just define your global variable and assign the object to it.

    I'm not quite sure what your sample code is trying to do here. It looks like the class and the objects of the class are getting mixed up. I can't tell if MyClass is the class and GlobalMyClass is a variable or the other way around. They are each used in both ways in different parts of the code snippet.

    Assuming that the class is "MyClass" and the global variable is globalMyClass you could do something like the following:

    class MyClass { .... }
    
    MyClass* globalMyClass;
    
    void App::OnLaunched(...) 
    {
       globalMyClass = new MyClass();
       ...
    }
    
    

    --Rob

    Wednesday, April 17, 2013 3:05 AM
    Owner
  • Thanks Rob.

    I am loosing my mind here. the StackPointerMyClass is there just to illustrate that the MyClass has nothing wrong with it. I was trying to attempt to discourage persons from saying maybe something is wrong with MyClass. Though I could have swore that I did try this, apparently I did not. GlobalMyClass is now accessible anywhere within the application that I include App.xaml.h and am using namespace TestWin8.

    In App.xaml.h

    #include "MyClass.hpp"
     

    namespace TestWin8
    {

     extern class GlobalMyClass *MyClass; // extern. INSIDE THE namespace

    .......

    App.xaml.cpp

    namespace TestWin8{ class GlobalMyClass *MyClass; // Declare it INSIDE the namespace} 

    void App::OnLaunched(LaunchActivatedEventArgs^ args)
    {class MyClass *StackPointerMyClass=(class MyClass *)NULL;

     // I Have StackPointerMyClass Here Just To Illustrate That
     // MyClass In Fact Functions. This All Works Just Fine.
     StackPointerMyClass=new MyClass();
     (StackPointerMyClass->Initialize)();
     (StackPointerMyClass->ListConnections)();

     // GlobalMyClass Is NULL (0000000 Pointer)
     GlobalMyClass=StackPointerMyClass;
     // GlobalMyClass Is Now assigned to StackPointerMyClass

    Thanks.

    • Marked as answer by RSullivan59 Wednesday, April 17, 2013 1:46 PM
    Wednesday, April 17, 2013 1:39 PM