473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

overloading typecast: my class -> string

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
3 4054
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
8
by: akolsen | last post by:
Hello there. This should be simple, but im having trouble anyway of getting it to work. I have a boxed object that i want to cast to its native type, but I would like to use reflection to do...
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.