overloading typecast: my class -> string

R

ryan.gilfether

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::eek:perator string()
{
return value;
}


// allow this class to be typecast into a char*, this works
ConfigFileValue::eek:perator 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?
 
A

Alf P. Steinbach

* (e-mail address removed):
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!
 
K

Kai-Uwe Bux

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::eek:perator string()
{
return value;
}


// allow this class to be typecast into a char*, this works
ConfigFileValue::eek:perator 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-
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
 
R

Rolf Magnus

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::eek:perator string()
{
return value;
}


// allow this class to be typecast into a char*, this works
ConfigFileValue::eek:perator 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-
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top