1

I have an error log class that I use throughout all the files in my project. It allows me to debug and have a greater understanding of what my code is doing. I declare it globally in main.cpp by saying Log errorLog and then use it accordingly. Now, I have been splitting files into multiple files and the way I have always gotten away with using my logger in multiple files by using "extern Log errorLog" and it works great.

Now, I am trying to figure out a way I can do this without simply including an extern at the top. A friend mentioned something about a singleton pattern. Can anyone explain this concept? Does anyone have a different concept that will work.

Thanks!

Satchmo Brown
  • 1,409
  • 2
  • 20
  • 47

1 Answers1

4

Stay with the extern and forget about singletons, asap please. Pretend you never heard of them. :) Also see this and this answer.

Another possibility is to pass your logger into every function and class that needs it, as even with extern, a global is still a global, and global variables are considered bad.

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397