By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

string assignment to char*

P: n/a
S S
I have a little question.

Usually we assign

String str = "hello";
char* p = str.Str(); //here Str() returns a char*, not a copy

or we do

char* p = strdup(str.Str()) // incase if scope of str is local, here we
do copy

But some of the times I have seen implementation like

char* p = str; //here I suppose p will get String& ,
does it implicitly converts it into char*? Is there some overloading
function in String class which I do not see any. Can someone tell.

Thanks
-SS

Sep 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
S S schrieb:
I have a little question.

Usually we assign

String str = "hello";
char* p = str.Str(); //here Str() returns a char*, not a copy

or we do

char* p = strdup(str.Str()) // incase if scope of str is local, here we
do copy

But some of the times I have seen implementation like

char* p = str; //here I suppose p will get String& ,
char* is always a pointer to char, never a reference to some class. If
you had actually &str in mind, I would doubt that -- pointers are
strongly typed in C++ (if you do not lie to the compiler via some
do_not_use_cast<>);
does it implicitly converts it into char*? Is there some overloading
function in String class which I do not see any. Can someone tell.
I don't know about a class 'String' (well, actually I have seen a
few...). I know about 'string', which is Standard C++.

You maybe want to look up in the header file for class String, there you
can see if String supports implicit casts to some types, or not.

std::string definitly does not, and for a reason. Implicit casts on
string types most times do more harm than good, and sometimes they are
just done plain wrong (best example of how not to do it: Microsoft's
_bstr_t).

Please note that non-standard c++ libraries are usually Off-Topic here.
Best regards,
-- Markus
Thanks
-SS
Sep 20 '06 #2

P: n/a
S S wrote:
I have a little question.

Usually we assign

String str = "hello";
char* p = str.Str(); //here Str() returns a char*, not a copy
Not if you are talking about the standard C++ string class std::string. For
one, the member function would be called str(), not Str(), and second, the
returned type is const char*, which can't be directly assigned to a char*.
or we do

char* p = strdup(str.Str()) // incase if scope of str is local, here we
do copy
strdup is not a standard function.
But some of the times I have seen implementation like

char* p = str; //here I suppose p will get String& ,
does it implicitly converts it into char*? Is there some overloading
function in String class which I do not see any. Can someone tell.
If you mean the standard string class, no. It doesn't provide a direct
conversion to char*.

Sep 20 '06 #3

P: n/a
Rolf Magnus schrieb:
S S wrote:

[...]
>String str = "hello";
char* p = str.Str(); //here Str() returns a char*, not a copy

Not if you are talking about the standard C++ string class std::string. For
one, the member function would be called str(), not Str(), and second, the
It's actually c_str()
returned type is const char*, which can't be directly assigned to a char*.

[...]>
-- Markus
Sep 20 '06 #4

P: n/a
Markus Grueneis wrote:
Rolf Magnus schrieb:
>S S wrote:

[...]
>>String str = "hello";
char* p = str.Str(); //here Str() returns a char*, not a copy

Not if you are talking about the standard C++ string class std::string.
For one, the member function would be called str(), not Str(), and
second, the

It's actually c_str()
Right. Thanks for the correction. Somehow, I was thinking about
stringstreams when writing this.

Sep 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.