I'm trying to use libconfig library to parse variables from an external configuration file.
On libconfig site it says: The class Config represents a configuration, and the class Setting represents a configuration setting. Note that by design, neither of these classes provides a public copy constructor or assignment operator. Therefore, instances of these classes may only be passed between functions via references or pointers.
I'm having difficulty creating a function that will return a Setting class reference (From my understanding returning reference to local object is frowned upon. But I have no idea how to declare a global reference given limitation above). At the bottom I have attached snippet of my attempt at coding this, however they are not working. I'm new to C++ and currently reading a textbook on it, but I'm still shaky on my handling of reference and pointer. I would appreciate if anyone can shed some light into what I'm doing wrong.
config.cfg
A=
{
min = 3;
}
Code
libconfig::Setting& GetKey(const char* filename, const char* method)
{
libconfig::Config cfg;
cfg.readFile(filename);
libconfig::Setting &root = cfg.getRoot();
libconfig::Setting &key = root[method];
// How can I return key?
}
libconfig::Setting &key = GetKey("config.cfg","A");
key.lookupValue("min",min);