By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

overloading typecast: my class -> string

P: n/a
I have a problem that I have been fighting for a while and haven't
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?

Feb 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* ry************@gmail.com:
Is there a better way for me to go about this using those instead?
Ordinary named member functions, e.g. 'asString'.

Also, remember to declare them 'const'.

Do avoid the C style casts, and for God's sake don't make casts
necessary by design!

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 4 '07 #2

P: n/a
ry************@gmail.com wrote:
I have a problem that I have been fighting for a while and haven't
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
Post a complete minimal example that shows the problem. What you posted
looks fine, the problem may just hide somewhere else.

BTW: I would expect conversion operators to be const:

operator std::string ( void ) const {
return ( value );
}

operator char const * ( void ) const {
return ( value.c_str() );
}
>
My current workaround is to do: string v = (string)(char*)config_file-
>>GetNext();
However, I'm pretty sure this is not ideal.
You are right, it looks clumsy.
Also, I have read that it is better to use the C++ style typcasts such
as
static_cast<>
dynamic_cast<>
reinterpret_cast<>
const_cast<>
True. But better is to use no casts at all. The overloaded conversion
operators should kick in automatically. Thus,

std::string v = config_file->GetNext();

should work.
Is there a better way for me to go about this using those instead?
Quite honestly, the above looks a little bit over-engineered to me. Why does
GetNext() not just return a std::string?
Best

Kai-Uwe Bux
Feb 4 '07 #3

P: n/a
ry************@gmail.com wrote:
I have a problem that I have been fighting for a while and haven't
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*
This should most likely be "operator string() const;" and "operator const
char*() const;". You should think again if you really want those as
conversion operators and not as regular functions.
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
What doy you mean by "does not work"? What happens instead of the expected?
It looks fine except for the missing 'const'.
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();
Avoid C style casts, and never just cast away constness unless you have a
really good reason.
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
It should work, unless GetNext returns a const ConfigFileValue. Btw: you
can completely remove the cast. The above conversion operator can be used
implicitly.
And again: Avoid C style casts.
However the compiler does not like that.
Please be more specific. Copy the error message to your posting. It might
not mean anything to you, but people here can offer better help if they see
it.
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.
You're pretty right about that.
Also, I have read that it is better to use the C++ style typcasts such
as
static_cast<>
dynamic_cast<>
reinterpret_cast<>
const_cast<>
Yes, it is.
Is there a better way for me to go about this using those instead?
The better way would be to not use any casts at all. The code you posted
doesn't need any.

Feb 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.