Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ returning an unknown type
    primarykey
    data
    text
    <p>I'm making a Configuration class that will hold my user application's configuration, and it reads from a file as strings.</p> <pre><code>class ConfigKey { public: string KeyLabel; //Will be used to identify this key string KeyValue; //The value bool IsEditable; //For developing uses only, I'm saving a few default and non editable keys for specific apps here }; class Configuration { public: void AddKey(char* keyLabel, char* keyValue, bool isEditable); private: vector&lt;ConfigKey&gt; configKeys; }; </code></pre> <p>So when I start the app, I read the config file line by line and add to my Config class:</p> <pre><code>//Constructor Configuration::Configuration() { //read from file, examples AddKey("windowWidth", "1024", false); AddKey("windowHeight", "768", false); } </code></pre> <p>Now I want to retrieve these values somewhere else for use in the app, is there a way I can leave the cast for the Configuration class? Something like this:</p> <pre><code>//In the Configuration class void* GetKey(char* keyLabel); //And when I call it, I'd like to do something like this: int windowAspectRatio = myApp.config.GetKey("windowWidth") / myApp.config.GetKey("windowHeight"); </code></pre> <p>The reason is so I don't have a bunch of stringstreams elsewhere in the code converting the config values before I can use them. I would save the configKey's type as well in the ConfigKey so it can auto-convert itself.</p> <p>Any advice or suggestions?</p> <p><strong>Edit for clarification:</strong></p> <p>I want to retrieve a configKey using this method:</p> <pre><code>//In the Configuration Class public: int GetKey(char* keyLabel) { //the value I saved in ConfigKey is a "string" type, but I'm converting it to Int before I return it //loop through the vector, find the keyLabel stringstream mySS(foundKey.KeyValue); int returnValue = 0; mySS &gt;&gt; returnValue; //converted the string to int return returnValue; //returned an int } </code></pre> <p>So elsewhere in the code I can call:</p> <pre><code>int myWidth = myConfig.GetKey("windowWidth"); //It's already converted </code></pre> <p>But I can have multiple configKeys that are either <em>int</em>, <em>float</em>, <em>bool</em> or even something else. I'm looking for a way to have the <strong>GetKey(char* keyLabel)</strong> to check for the keyType, then convert it, then return it.</p> <p>Or any advice on a better solution!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload