473,391 Members | 1,542 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,391 software developers and data experts.

String^, const char*, std::string, and c_str( )

I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the
new C++/CLI syntax rather than /clr:oldSyntax.

Much of our managed code is concerned with interfacing to native C++ code
(ie. wrappers etc). In Managed C++ there was an automatic conversion between
const char* and String^. This was useful to us for two reasons:

1. We declared our string constants as eg. const char* const c_My_Constant =
"blah", and these strings could be easily utilised (ie. assigned) to either
a std::string or a String^.
2. We could easily convert/assign std::string to String^ using the .c_str( )
method.

Both of these techniques no longer work in C++/CLI. The implicit conversion
has now disappeared.

Now, in case (2) I have no problem changing all this code to instead use a
"to_string( )" kind of method that converts std::string to String^. It
probably even has the benefit of making the code more readable.

I could obviously do the same thing for case (1) but it irks me to have to
convert a "constant". Unfortunately, however, I have numerous situations
where the same constant will be used by both native and managed C++.

Is there a better way to achieve this?

Thanks

Kevin
Feb 20 '07 #1
3 9501
Kevin Frey wrote:
Now, in case (2) I have no problem changing all this code to instead use a
"to_string( )" kind of method that converts std::string to String^.
You don't need a to_string() function. You can construct a String
directly from a const char*:

std::string s;
String^ managed_str = gcnew String(s.c_str());
I could obviously do the same thing for case (1) but it irks me to have to
convert a "constant".
You certainly don't have to convert a literal, you can assign it directly:

String^ ms2 = "Hello";

A const char* is not a literal constant, and requires conversion, even
in VC++ 2003 and /clr:oldSntax. const char* is using 1 byte per
character, while String^ is using 2 bytes. Also, const char* uses
unmanaged memory, while String^ is garbage collected. They're not
compatible. In the old MC++, this conversion was implicit, but it was a
conversion function nonetheless.
Unfortunately, however, I have numerous situations
where the same constant will be used by both native and managed C++.
You can not really have the same code compile for .NET and ISO C++. The
syntax is vastly different, just think about class vs ref class / value
class, or enum vs enum class, etc. You can compile ISO C++ syntax to
managed code, but ISO C++ declarations won't be accessible from outside
of the assembly, and ISO C++ types will be unmanaged types.

The following, however, will always work with both ISO C++ and C++/CLI,
without 0 overhead:

#define MY_STR "Hello"

In the old MC++, you use to have to write S"Hello", which was much
worse. You no longer have to do that, all you need is the double quotes.
So the situation in C++/CLI is not worse, but much better than it used
to be.

In the old MC++, every time you did this, you had a conversion overhead,
whether you realized that or not:

const char* my_str = "Hello";
String* managed_str = my_str;

That's exactly the same overhead as the following in C++/CLI:

const char* my_str = "Hello";
String^ managed_str = gcnew String(my_str);

Tom
Feb 20 '07 #2
Hi Tom,
You don't need a to_string() function. You can construct a String directly
from a const char*:

std::string s;
String^ managed_str = gcnew String(s.c_str());
Sure, I knew this. But to my eyes it is ugly. It expresses the mechanics of
the conversion every time (and hence in every place) it is expressed. I
would therefore much rather have:

String^ managed_str = to_string( s );

Which hides the mechanics of the conversion. Inlining the code should result
in no performance hit either.
You certainly don't have to convert a literal, you can assign it directly:

String^ ms2 = "Hello";
Yes, but in practice we don't sprinkle lots of "magic strings" around our
code like your example, hence it doesn't help us much, and most examples
found on the internet seems to focus on this type of example. If there are
constants involved they are declared as const char* const constants. We
could use preprocessor macros as you suggest but once again it seems ugly,
and more importantly, regressive to have to do so.
A const char* is not a literal constant, and requires conversion, even in
VC++ 2003 and /clr:oldSntax. const char* is using 1 byte per character,
while String^ is using 2 bytes. Also, const char* uses unmanaged memory,
while String^ is garbage collected. They're not compatible. In the old
MC++, this conversion was implicit, but it was a conversion function
nonetheless.
Yes, I did say that I understood that there was an automatic/implicit
conversion going on. And now there isn't in C++/CLI. I am not concerned with
the performance implication - after all there *used* to be a conversion
going on anyway. My interest was determining whether I could get the
conversion for free (coding-wise), rather than being explicit about it.
>Unfortunately, however, I have numerous situations where the same
constant will be used by both native and managed C++.

You can not really have the same code compile for .NET and ISO C++. The
syntax is vastly different, just think about class vs ref class / value
class, or enum vs enum class, etc. You can compile ISO C++ syntax to
managed code, but ISO C++ declarations won't be accessible from outside of
the assembly, and ISO C++ types will be unmanaged types.
You have over-extrapolated the extent of my comment. I was not trying to
re-use the same code, I was trying to re-use some of the same string
constants. Nothing more, nothing less. Perhaps a filename that is used by
both managed and native code, as an example.

Thanks for your input - it seems to be confirming to me that I'm going to
have to perform some explicit conversions along the way.

Kevin
Feb 20 '07 #3
Kevin Frey wrote:
>>String^ managed_str = gcnew String(s.c_str());

Sure, I knew this. But to my eyes it is ugly.
I understand your reasoning, and you are right. The implicit conversion
was quite convenient in MC++.
You have over-extrapolated the extent of my comment.
Sorry about that.
I was not trying to
re-use the same code, I was trying to re-use some of the same string
constants. Nothing more, nothing less.
But you can't even reuse string constants very easily. For managed
types, the syntax is:

public ref struct MagicStrings
{
literal String^ name1 = "value1";
literal String^ name2 = "value2";
};

In native C++, it's:

// Into the .h file:
extern const TCHAR* name1;
extern const TCHAR* name2;

// Into the .cpp file:
const TCHAR* name1 = _T("value1");
const TCHAR* name2 = _T("value2");

Tom
Feb 21 '07 #4

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

Similar topics

23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
5
by: sean345 | last post by:
While compiling my program I get the following g++ error: error: no matching function for call to `ErrorLog::file_error(std::string&, std::string&, const char)' note: candidates are: void...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
3
by: Rick Helmus | last post by:
Hello all In a few classes I have overloaded functions for C style strings and STL strings like this class SomeClass { void f(const char *s); void f(const std::string &s); };
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.