found a good solution for. Forgive me, but my C++ is really rusty.
I have a custom config file class:
class ConfigFileValue
{
public:
operator string(); // allow this class to be typecast into a string
operator char*(); // allow this class to be typecast into a char*
private:
string value;
};
class ConfigFile
{
public:
ConfigFile(); // standard constructor
ConfigFileValue GetNext();// returns the next value
};
Here is how I am overloading the typcast operators:
// allow this class to be typecast into a string, does not work
ConfigFileValue::operator string()
{
return value;
}
// allow this class to be typecast into a char*, this works
ConfigFileValue::operator char*()
{
char* ch = (char*)value.c_str();
return ch;
}
If I make a call to ConfigFile::GetNext() it returns a value of type
ConfigFileValue. There are times
when I want to typecast that value to a string, for example:
string v = (string)config_file->GetNext(); // does not work
However the compiler does not like that. I was able to overload the
typecast operator for
other types with success, for example:
char* c = (char*)config_file->GetNext(); // this works
My current workaround is to do: string v = (string)(char*)config_file-
>GetNext();However, I'm pretty sure this is not ideal.
Also, I have read that it is better to use the C++ style typcasts such
as
static_cast<>
dynamic_cast<>
reinterpret_cast<>
const_cast<>
Is there a better way for me to go about this using those instead?