Re: std::string <--> System::String*
- From: ricecake@xxxxxxxxxxxx (Marcus Kwok)
- Date: Mon, 20 Feb 2006 10:44:47 -0800
Jochen Kalmbach [MVP] <nospam-Jochen.Kalmbach@xxxxxxxxx> wrote:
Hi Marcus!
Why not use:
struct Conv
{
char *szAnsi;
Conv(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{}
~Conv()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
Then you can simply write:
std::string ss = Conv(s);
Nice, now this eliminates the need for a second wrapper function :)
class MarshalNetToStdString {
public:
MarshalNetToStdString(System::String* s)
: cp(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
{ }
~MarshalNetToStdString()
{
System::Runtime::InteropServices::Marshal::FreeHGlobal(static_cast<System::IntPtr>(cp));
}
operator const char*() const
{
return cp;
}
private:
char* cp;
MarshalNetToStdString(const MarshalNetToStdString&);
MarshalNetToStdString& operator=(const MarshalNetToStdString&);
};
However, thinking about it, I was reminded of the "rule of 3" (in
essence, it says that if you provide any of {destructor, copy
constructor, assignment operator} then you probably need to provide all
three of them). I felt that it wouldn't really make much sense to be
copying these objects around, so I made the copy constructor and
assignment operator private to prevent the compiler from generating
default ones that will only perform shallow copies. The default
constructor does not get auto-generated since I have a user-defined
constructor, so I don't have to worry about that one.
Please see if the following train of thought sounds logical:
I only intend to use this class in the context of
System::String* s = new System::String("...");
std::string ss = MarshalNetToStdString(s);
In other words, create a temporary object (which gets destroyed at the
end of the statement) to do the conversion. In order to avoid the
overhead of creating/destroying an object every time, I thought about
making static member functions to do the conversion, but then I realized
that this would make the memory management much more complex, so I
decided against it. Plus, this class seems pretty lightweight so I
don't think the overhead is too great compared to the safety it
provides.
--
Marcus Kwok
.
- Follow-Ups:
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- References:
- std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Bronek Kozicki
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Jochen Kalmbach [MVP]
- Re: std::string <--> System::String*
- From: Jochen Kalmbach [MVP]
- Re: std::string <--> System::String*
- From: Jochen Kalmbach [MVP]
- Re: std::string <--> System::String*
- From: Jeff F
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Jochen Kalmbach [MVP]
- Re: std::string <--> System::String*
- From: Jeff F
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Marcus Kwok
- Re: std::string <--> System::String*
- From: Jochen Kalmbach [MVP]
- std::string <--> System::String*
- Prev by Date: Re: std::string <--> System::String*
- Next by Date: Re: std::string <--> System::String*
- Previous by thread: Re: std::string <--> System::String*
- Next by thread: Re: std::string <--> System::String*
- Index(es):
Relevant Pages
|