Declaring GLOBAL managed objects

P

Peter Oliphant

OK. I created a managed class. Now I want to create a global pointer to an
instance of the class that the entire application can reference.

Yet, when I do something like:

MyManagedClass* my_instance ;

as a global variable (i.e., it's not in a process of class or struct, or
etc.) it tells me something like 'managed class instances can't be global'.
That seems pretty restrictive...

What can I do to create global instances of a managed class?

The exact error message is:

error C3145: 'form' : cannot declare a global or static managed type object
or a __gc pointer

for this code:

PAO_Form* form ; // which is not in anything, just part of the .CPP file
that contains main()

[PAO_Form is a __gc class]

Thanx in advance for any help! : )
 
A

adebaene

Peter said:
OK. I created a managed class. Now I want to create a global pointer to an
instance of the class that the entire application can reference.

Yet, when I do something like:

MyManagedClass* my_instance ;

as a global variable (i.e., it's not in a process of class or struct, or
etc.) it tells me something like 'managed class instances can't be global'.
That seems pretty restrictive...
Yes and no... It prevents from using some bad designs (such as having
global variables ;-)
What can I do to create global instances of a managed class?

The global should be an unmanaged object referencing the managed object
--> gcroot is what you are looking or.

Arnaud
MVP - VC
 
V

Vladimir Nesterovsky

Yes and no... It prevents from using some bad designs (such as having
global variables ;-)

You can declare public static member field or property of MyManagedClass*
type on managed class.
E.g.

public __gc class MyManagedClass
{
static MyManagedClass()
{
// Initialize it
my_instance = ...
}
public:
static MyManagedClass *my_instance;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top