I have just started learning C++ after being a Flex developer for many years. I typically build native applications that operate like custom PPT presentations. That is my space. After taking a bunch of online C++ courses, I felt it was time to take a break from watching videos and start writing some code to help soak in the learning and continue my education by putting things into practice.
I wanted to focus on building a simple class/library that has practical meaning to what I do, and I decided to build a class called "ApplicationPreferences". This class will ultimately have things like a file path object, application ID object, etc.... but what I am focusing on first is a vector object called "preferences". This object would be of another custom class type "GenericPreference". GenericPreference is a simple class that stores a key/value pair to represent a user preference. For instance, a key could be "WindowWidth" and the value could be "1920". Or the key could be "SlideHoldTime" and the value could be "5.5" (as in seconds). So I created multiple constructors for the class so that value pairs of different types could be supported. I figured the "GenericPreference" class would support the basic types: bool, int, float, double, and string (from the standard string class).
Now comes the fun part that I am struggling on best way top handle. I now want to be able to build new Preference classes that extend the GenericPreference class to support other types for the value part of the key/value pair. The value part of the equation could itself be a complex class of some sort. Perhaps it is a rectangle so that rather the key "WindowWidth" it can be "WindowSize". Either way, I would like the ability to extend the GenericPreference class over time and allow the "preferences" object from my "ApplicationPreferences" class to support classes extended from "GenericPreference".
Any tips or help on what to use and/or how to go about the planning of this would be greatly appreciated!! Certainly not looking for full solutions, rather some hints or opinions that can help guide me :)
A general solution would be to use the main object class from a JSON parsing library (e.g. https://github.com/nlohmann/json ). That's completely generic.
Although most people would rather copy the contents of such an object into a specialized class to be able to say options.path rather than options["path"]. The former has the extra advantage that it can be type-checked at compile time.
I appreciate the suggestion to the JSON parsing library. Ultimately, my decisions will be to leverage existing libraries to do standard stuff like this, rather than build my own. However, I am forcing myself to do this as sort of a "homework assignment" to myself, in an educational effort to fully understand the core fundamentals of C++. Thanks again for your input.