G
Gordon Jahn
Hi all,
I'm trying to define a class that I can dump several known types into
and it will track what was dropped in and provide methods to get the
data back again. Anwyay, after 3 days, I'm branching out for advice!
Included below is my full SettingsManagerSetting class and a main()
which instantiates two instances of it. The problem is that the char
designator is valid only during the constructor phase, which is weird
because it's a member variable being called by value (I think). The
cout's will show that when the class is accessed from the main() again,
the designator data has become garbage.
Does anyone know why on earth this would happen, or more importantly,
how I can make the class actually store data?
I've tried this on both Linux and Windows with GCC (g++) 3.2 and 3.3.1
respectively with the same results, so I reckon it's something I'm doing.
Cheers in advance,
Gordon Jahn
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class SettingsManagerSetting {
char designator;
void *value;
public:
SettingsManagerSetting(string value) { SettingsManagerSetting(&value,
(char) 'S');};
SettingsManagerSetting(int value) { SettingsManagerSetting(&value,
(char) 'I');};
SettingsManagerSetting(float value) { SettingsManagerSetting(&value,
(char) 'F');};
string getStringValue();
int getIntValue();
float getFloatValue();
char getDesignator() { return designator; };
void printString();
protected:
SettingsManagerSetting(void *newValue, char typeDesignator) {
value = newValue;
designator = typeDesignator;
cout << "SMS: " << getStringValue() << "\n";
};
void *getValue() { return value; };
};
string SettingsManagerSetting::getStringValue() {
cout << "Running getSV, designator: " << getDesignator() << "\n";
switch (getDesignator()) {
case 'S': {
cout << "Howdy... we're in S.\n";
string response = *((string *) getValue());
return response;
}
case 'F': {
cout << "Howdy... we're in F.\n";
stringstream tempstring;
tempstring << getFloatValue();
return tempstring.str();
}
case 'I': {
cout << "Howdy... we're in I.\n";
stringstream tempstring;
tempstring << getIntValue();
return tempstring.str();
}
default: {
return string("I dunno... - couldn't resolve type");
}
}
}
int SettingsManagerSetting::getIntValue() {
int response = *((int *) getValue());
return response;
}
float SettingsManagerSetting::getFloatValue() {
float response = *((float *) getValue());
return response;
}
void SettingsManagerSetting:rintString() {
cout << getStringValue() << "\n";
}
int main(int argc, char *argv[]) {
if (argc == 2) {
string ava("a");
int bva = 10;
SettingsManagerSetting *bv = new SettingsManagerSetting(bva);
bv->printString();
cout << "Main: " << (*bv).getStringValue() << "\n";
SettingsManagerSetting *av = new SettingsManagerSetting(ava);
cout << "Main: " << (*av).getStringValue() << "\n";
} else {
cout << "Usage: SettingManager [LookupValue]\n";
}
return 0;
}
I'm trying to define a class that I can dump several known types into
and it will track what was dropped in and provide methods to get the
data back again. Anwyay, after 3 days, I'm branching out for advice!
Included below is my full SettingsManagerSetting class and a main()
which instantiates two instances of it. The problem is that the char
designator is valid only during the constructor phase, which is weird
because it's a member variable being called by value (I think). The
cout's will show that when the class is accessed from the main() again,
the designator data has become garbage.
Does anyone know why on earth this would happen, or more importantly,
how I can make the class actually store data?
I've tried this on both Linux and Windows with GCC (g++) 3.2 and 3.3.1
respectively with the same results, so I reckon it's something I'm doing.
Cheers in advance,
Gordon Jahn
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class SettingsManagerSetting {
char designator;
void *value;
public:
SettingsManagerSetting(string value) { SettingsManagerSetting(&value,
(char) 'S');};
SettingsManagerSetting(int value) { SettingsManagerSetting(&value,
(char) 'I');};
SettingsManagerSetting(float value) { SettingsManagerSetting(&value,
(char) 'F');};
string getStringValue();
int getIntValue();
float getFloatValue();
char getDesignator() { return designator; };
void printString();
protected:
SettingsManagerSetting(void *newValue, char typeDesignator) {
value = newValue;
designator = typeDesignator;
cout << "SMS: " << getStringValue() << "\n";
};
void *getValue() { return value; };
};
string SettingsManagerSetting::getStringValue() {
cout << "Running getSV, designator: " << getDesignator() << "\n";
switch (getDesignator()) {
case 'S': {
cout << "Howdy... we're in S.\n";
string response = *((string *) getValue());
return response;
}
case 'F': {
cout << "Howdy... we're in F.\n";
stringstream tempstring;
tempstring << getFloatValue();
return tempstring.str();
}
case 'I': {
cout << "Howdy... we're in I.\n";
stringstream tempstring;
tempstring << getIntValue();
return tempstring.str();
}
default: {
return string("I dunno... - couldn't resolve type");
}
}
}
int SettingsManagerSetting::getIntValue() {
int response = *((int *) getValue());
return response;
}
float SettingsManagerSetting::getFloatValue() {
float response = *((float *) getValue());
return response;
}
void SettingsManagerSetting:rintString() {
cout << getStringValue() << "\n";
}
int main(int argc, char *argv[]) {
if (argc == 2) {
string ava("a");
int bva = 10;
SettingsManagerSetting *bv = new SettingsManagerSetting(bva);
bv->printString();
cout << "Main: " << (*bv).getStringValue() << "\n";
SettingsManagerSetting *av = new SettingsManagerSetting(ava);
cout << "Main: " << (*av).getStringValue() << "\n";
} else {
cout << "Usage: SettingManager [LookupValue]\n";
}
return 0;
}