[C++/CLI] Briser l'encapsulation ?
Bonjour,
Il n'y a pas de forum spécifique pour le C++/CLI, donc je me permet de poster ici.
Bon alors... Regardez le résultat de l'éxécution de ce code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
/* L'interface de la classe de test */
public interface class ITest{<div style="margin-left:40px">System::Int32^ getVal();</div>};
/* la classe de test */
public ref class Test : ITest{<div style="margin-left:40px">private:<div style="margin-left:40px">System::Int32^ val;
Test(System::Int32^ i)
:val(i)
{}</div>public:<div style="margin-left:40px">static ITest^ create(System::Int32^i){<div style="margin-left:40px">return gcnew Test(i);</div>}
virtual System::Int32^ getVal(){<div style="margin-left:40px">return this->val;</div>}</div></div>};
/* La fonction principale */
void main(void){<div style="margin-left:40px">ITest^ test = Test::create(1);
int %j = *(test->getVal());
j = 2;
System::Console::WriteLine("val = " + test->getVal());</div>} |
Résultat :
Ca craint un peu non ? en deux lignes on brise l'encapsulation de la classe Test...