lundi 27 janvier 2020

In C++11, can a global object be instantiated by a function call?

There are two files ObjectCreator.h and ObjectCreator.cpp that declare and define the following function.

In ObjectCreator.h

#include "Object.h"

Object* Create_Object();

In ObjectCreator.cpp

#include "ObjectCreator.h"

Object* Create_Object()
{
   return new Object();
}

The question is, can I create a global instance of Object in the following way in a main.cpp file?

In main.cpp

#include "ObjectCreator.h"

static Object* object = Create_Object();

int main()
{
   // Nothing here
}

The best way I could ask this question was an example and I have verified this is possible. I am just not sure if it is possible in C++11 or what the technical term for this is. I am aware objects can be instantiated globally, I am just not sure if they can be done in this specific way in C++11.

Aucun commentaire:

Enregistrer un commentaire