| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"sqwirx" |
| Date: |
20 Dec 2003 10:14:20 PM |
| Object: |
replacing c_str with operator asterix |
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?
.
|
|
| User: "Peter van Merkerk" |
|
| Title: Re: replacing c_str with operator asterix |
21 Dec 2003 12:17:27 PM |
|
|
"sqwirx" <sqwirx@dr.com> wrote in message
news:954b2fab.0312202014.4ad505c3@posting.google.com...
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.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
.
|
|
|
|
| User: "Cy Edmunds" |
|
| Title: Re: replacing c_str with operator asterix |
20 Dec 2003 10:43:43 PM |
|
|
"sqwirx" <sqwirx@dr.com> wrote in message
news:954b2fab.0312202014.4ad505c3@posting.google.com...
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::operator * ()?
--
Cy
http://home.rochester.rr.com/cyhome/
.
|
|
|
|

|
Related Articles |
|
|