1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public ref class toto
{
public:
IntPtr handle;
void Attach(String ^% v)
{
v="affection par référence"; // a ce moment s est egal à:affection par référence, normal !!!
GCHandle gcHandle = GCHandle::Alloc(v);
handle = static_cast<IntPtr>(GCHandle::ToIntPtr(gcHandle));
}
void affect(String ^str)
{
String ^ targetStr = safe_cast< String ^ >(static_cast<GCHandle>(handle).Target);
// ici targetStr == "affection par référence"
targetStr=str; // a ce moment s est toujours egal à :affection par référence et pas à toto
}
};
int main(array<System::String ^> ^args)
{
String ^s="abc";
toto ^c = gcnew toto;
c->Attach(s);
c->affect("toto"); // n'affecte pas s
Console::WriteLine("s==toto {0}",s=="toto"); // affiche false
} |
Partager