Static Pointer to a class that is used globally

If I need a static pointer to a class that is used globally(multiple files), and I only want to allocate memory once.

One way is to create a function that returns a static pointer of type class and call it where ever you need this pointer. My question is there another way to do this like with a header file and include the header file where you need to use the object of type class.

static class* function
{
static class c;
if (c == NULL)
{
c = new class;
}
return c
}
Last edited on
1
2
3
4
5
MyClass *getMyClassInstance()
{
    static MyClass inst;
    return &inst;
}
Though I would recommend returning a reference or const reference.
Last edited on
Topic archived. No new replies allowed.