I use a global variable (an object of a class) and define it as follows:
//foo.h
extern class_name obj_name;
//foo.cpp
class_name obj_name("directory of a .ttf file");
I included foo.h in my main.cpp where my main() function resides.
The problem is that I am not very comfortable with initializing it there , since it is not inside a function. It’s out in the open. I would have preferred calling an “init()” function that initializes the object like:
object_name = class_name(parameters);
from the main() function.
But the class is a 3rd party one and it doesn’t accept objects being created without us providing parameters for the constructor. Maybe it doesn’t have a default no-parameter constructor.
Is it good coding style to do what I did above?
No.
Don’t use globals in C++ without very good reason.
This is especially true if your global is of a 3rd party type.
Consider using something nicer like a factory method allowing lazy initialization.
Or just pass the object around to functions that need it.
Also consider wrapping that 3rd party class with your own – only providing the minimal interfaces needed to test it, and use a pointer to that abstract base class. (Or if the 3rd party library supports it – use the most minimal interface they provide that you can get away with using)