473,407 Members | 2,326 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,407 software developers and data experts.

string assignment to char*

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

Similar topics

13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
6
by: PX | last post by:
Greetings, Please take a look at following code: " struct STR { char string; ... /* something else */ };
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
8
by: Fred Hebert | last post by:
I have a function that requires a LPCTSTR parameter. I have the value I want to pass to it in a TextBox->Text field. Is there any way to do it in a single assignment. e.g. DWORD...
5
by: shan_rish | last post by:
Hi Group, When i compile the following program it compiles successfully, but crashes while executing. I am trying to assign a NULL char pointer to a string. The error message is ...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
7
by: LuB | last post by:
I have to convert a wide (16bit) char to a standard (8bit) char. Please nevermind that I am using WideCharToMultiByte ... but my question is as follows: Does this approach make any sense? My...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
23
by: =?iso-8859-1?q?Santiago_Urue=F1a?= | last post by:
Hi, I tried to return a pointer to a constant string, but the compiler gives the following warning if a cast is not used: warning: assignment from incompatible pointer type This is the code:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.