mercredi 30 décembre 2015

using smart pointers as global variables

Let's say I have a program in which I must use a global variable (of some class type).

I would like to be able to use smart pointers so I won't have to worry about deleting it.

in some file Common.hpp file I have the declaration:

extern unique_ptr<CommandBuffer> globalCommandBuffer;

and in my main.cpp:

#include "Common.hpp"

int main(int argc, char* argv[]) {   
   globalCommandBuffer(new CommandBuffer());
}

this creates many compilation errors. so obviously I'm doing it wrong.

my questions are:

  • is it a good design choice to use smart pointers for global variables?
  • if so, what is the correct way of doing so?
  • which smart pointer is preferable?

Aucun commentaire:

Enregistrer un commentaire