Is C # ref int foo implemented as C ++ int ** foo?

I spoke with a colleague about this yesterday, and it made me think .Net to follow the link.

// C#
class Foo {}

static void Test(ref Foo foo) { ... };
static void Main()
{ 
    Foo f;
    Test(ref foo);
}

It should be implemented with double indirection, because we change the value of the pointer. Since all reference types are links (pointers)

// C#
static void Test(Foo foo) { ... }
static void Test(ref Foo foo) { ... };

mean something like

// C++
void Test(Foo *foo);
void Test(Foo **foo);

But if it's a VALUE type , we don’t need double indirection. Therefore, I wonder if

// C#
static void Test(ref int bar) { ... }

becomes

// C++
void Test(int *bar);
// or
void Test(int **bar);

1/29/10 : , , , , , ++ , . , CLR JIT . , , , , , , .

+3
7

#,

void M(ref int f) { }

int x = 123;
M(ref int x):

?

, "x f ".

CLR, f " , ". x M.

, ++, , & int - , int.

?

+5

ref - . ,

int x;
Foo(ref x);

Foo

void Foo(ref int y)

x y .

, void Foo(ref int y) # void Foo(int &y) ++.

+6

.


Foo foo = new Foo();

" ", Foo. Foo .

Bar(Foo x) { x = new Foo(); }

Foo, (, foo), .

Foo x. Foo .

Bar(foo);

Bar , Foo, x - .

, int Foo, , , , , .


Qux(ref Foo y) { y = new Foo(); }

Foo&, , , Foo.

Qux(ref x);

Qux, y x - .

Qux Foo , y ( Foo). Foo .

, Foo int, , , , , , .

+5

, * . , , :

void Test(Foo &bar);
0

, int .

0
  • MyClassMyClass* (, , shared_ptr<MyClass>)
  • ref&

#:

void doSomething(int myInt);
void doSomethingElse(ref int myIntRef);
void doSomething(Foo foo);
void doSomethingElse(ref Foo fooRef);

++:

void doSomething(int myInt);
void doSomethingElse(int& myInt);
void doSomething(Foo* fooPtr);
void doSomething(Foo*& fooPtrRef);
0

, ++/CLI. : Expert ++/CLI: NET Visual ++, . 83.

Foo^ " Foo", UnmanagedFoo*.

int% Foo^% " ", int& UnmanagedFoo*&.

0

Source: https://habr.com/ru/post/1730418/


All Articles