Reputation: 1437
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!
Upvotes: 1
Views: 1012
Reputation: 131829
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.
Upvotes: 4