replacing c_str with operator asterix

S

sqwirx

I am using a String that has a member function c_str(). Because I
think c_str() reduces the readability of the code and I don't want
implicit conversions, I'm concidering to add a "char const *
operator*()". When you look at String as a fancy char* smartpointer
then it makes perfectly sense to dereference it with operator*.

What you think of it?
 
C

Cy Edmunds

sqwirx said:
I am using a String that has a member function c_str(). Because I
think c_str() reduces the readability of the code and I don't want
implicit conversions, I'm concidering to add a "char const *
operator*()". When you look at String as a fancy char* smartpointer
then it makes perfectly sense to dereference it with operator*.

What you think of it?

You can hire thousands of programmers who will immediately understand
std::string::c_str(). Who is going to understand String::eek:perator * ()?
 
P

Peter van Merkerk

sqwirx said:
I am using a String that has a member function c_str(). Because I
think c_str() reduces the readability of the code and I don't want
implicit conversions, I'm concidering to add a "char const *
operator*()". When you look at String as a fancy char* smartpointer
then it makes perfectly sense to dereference it with operator*.

What you think of it?

I agree with Cy, this not a good idea; even many alternative string classes
have a c_str() member. Inventing your own conventions won't make your code
clearer to other people. Unintuitive operator overloading only obfuscates
your code. So personnaly I'd stick with the standard conventions.

I have seen string classes with implicit conversion operators to const char*
(e.g. MFC's CString class) so you could write:

CString str = "Hello";
const char* ptr = str;

Those type conversion operators may seem appealing at first, but classes
with type conversion operators can yield unexpected results. So I tend to
avoid those to.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top