String^, const char*, std::string, and c_str( )
- From: "Kevin Frey" <kevin_g_frey@xxxxxxxxxxx>
- Date: Wed, 21 Feb 2007 08:52:22 +1100
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
.
- Follow-Ups:
- Re: String^, const char*, std::string, and c_str( )
- From: Tamas Demjen
- Re: String^, const char*, std::string, and c_str( )
- Prev by Date: Re: Screen Saver Fails to Start
- Next by Date: Re: RC2104 error on #ifdef
- Previous by thread: Re: Screen Saver Fails to Start
- Next by thread: Re: String^, const char*, std::string, and c_str( )
- Index(es):
Relevant Pages
|