473,467 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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?
Jul 22 '05 #1
2 2917
"sqwirx" <sq****@dr.com> wrote in message
news:95**************************@posting.google.c om...
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/
Jul 22 '05 #2
"sqwirx" <sq****@dr.com> wrote in message
news:95**************************@posting.google.c om...
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
2
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw...
6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
18
by: Metro12 | last post by:
In the <basic_string.h>, I find the implementation of these two functions. But I can't understand the difference between them. Please give me some help! //basic_string::c_str() const _CharT*...
2
by: Mike C | last post by:
I have an account list. There's an asterix in the account name if it has closed. For example, Acm* Corp. I don't agree with this design but that's what our company came up with. Anyway, I'm trying...
2
by: Mark P | last post by:
Consider a class in which I redefine operator new(std::size_t): struct A { void* operator new(std::size_t size) {/* my implementation */} }; This has the consequence of hiding the placement...
12
by: shyam | last post by:
Hi I have a program within which i have the following statement. sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str( ),SUFFIX.c_str( ) ); Here PREFIX, MIDDLE and SUFFIX are all const...
2
by: dale5804 | last post by:
hi im using the code below to password protect a button on a form.... is there any code i can use within the below to *asterix* out the password when typed in cheers. Private Sub...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.