如何处理 C++/CLI 中的句柄?

发布于 2024-07-13 03:01:36 字数 908 浏览 7 评论 0原文

我知道我问这个问题也不太正确。 请帮助我更好地提出我的问题。

我很难让自己的注意力集中在句柄上——在某些方面,它看起来像指针。 但与指针不同的是,我似乎可以直接向句柄变量赋值,并且它影响底层数据值,而不是句柄本身。

测试代码清楚地表明,无论我使用句柄还是“取消引用”句柄来获取数据,我都会获得相同的值。 显然,这不适用于非托管指针。 我不明白什么?

#include <iostream>

int main()
{

  int ^y;
  int ^a, ^b, ^c;
  long x;

   y= gcnew int(100);
   a=y;
   b=y;
   c=y;

   c= gcnew int(200);
   b= 300;

   System::Console::WriteLine(y); // returns 100 (instead of something pointer-like)
   System::Console::WriteLine(*y); // also returns 100

   System::Console::WriteLine(a); // 100
   System::Console::WriteLine(b); // 300
   System::Console::WriteLine(c); // 200 

   x = static_cast<long>(y);
   *y = 10;

   System::Console::WriteLine(x); // 10
   System::Console::WriteLine(y); // 10
   System::Console::WriteLine(*y); // 10

  }

编辑添加——我怀疑 WirteLine 可能已经为我完成了取消引用,但我预计静态转换不会很长。 这也与自动拆箱有关吗?

I know I'm not asking this quite right, either. Please help me better form my question.

I'm having a bit of a hard time getting my mind wrapped around handles -- in some ways, it looks like pointers. But unlike pointers, it seems like I can assign values directly to the handle variable, and it affects the underlying data value, not the handle itself.

The test code clearly shows that I get the same value whether I use the handle, or if I "dereference" the handle to get to the data. Clearly, this wouldn't work with unmanaged pointers. What am I not understanding?

#include <iostream>

int main()
{

  int ^y;
  int ^a, ^b, ^c;
  long x;

   y= gcnew int(100);
   a=y;
   b=y;
   c=y;

   c= gcnew int(200);
   b= 300;

   System::Console::WriteLine(y); // returns 100 (instead of something pointer-like)
   System::Console::WriteLine(*y); // also returns 100

   System::Console::WriteLine(a); // 100
   System::Console::WriteLine(b); // 300
   System::Console::WriteLine(c); // 200 

   x = static_cast<long>(y);
   *y = 10;

   System::Console::WriteLine(x); // 10
   System::Console::WriteLine(y); // 10
   System::Console::WriteLine(*y); // 10

  }

Edit to add -- I suspected that WirteLine might have done the dereferencing for me, but I would have expected the static cast to long would not. Is this related to autounboxing as welll?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ι不睡觉的鱼゛ 2024-07-20 03:01:36

有点遗憾的是 C++/CLI 允许这种语法。 int 类型是值类型,帽子用于引用类型。 您的“y”变量不存储 int,它存储 System::Object。 当您分配它时,编译器会自动生成装箱指令。 Console::WriteLine() 在显示装箱 int 对象的值时没有问题。

经验法则:当它是类对象时使用帽子,对于简单值类型省略它。 避免引用类型的堆栈语义(省略帽子,以便在作用域结束时自动调用析构函数),直到您真正理解值类型和引用类型之间的差异以及为什么 Dispose() 很重要。

It's a bit sad that C++/CLI allows this syntax. The int type is a value type, the hat is used for reference types. Your "y" variable does not store an int, it stores System::Object. The compiler automatically generates a boxing instruction when you assign it. Console::WriteLine() has otherwise no problem displaying the value of an object that's a boxed int.

The rule of thumb: use the hat when it is a class object, omit it for simple value types. Avoid the stack semantics of a reference type (omitting the hat so it automatically calls the destructor when the scope ends) until you really grok the difference between value and reference types and why Dispose() is important.

枫林﹌晚霞¤ 2024-07-20 03:01:36

使用 WriteLine 的副作用。

只是“y”被视为“对象引用”,并且可能被询问 IFormattable,然后调用 ToString() 。 '*y' 正在传递一个 int。

要测试这一点,请创建另一个函数调用 Foo(int ^) 并查看允许传入的内容,然后将其更改为“Foo(int)”。

我认为您只是被 WriteLine 的“可变参数”性质所愚弄。

Side effect of using WriteLine.

just 'y' is being treated like an 'object reference' and probably being interrogated for IFormattable, and then having ToString() called on it. '*y' is passing an int.

To test this, make another function call Foo(int ^) and see what you are allowed to pass in, and then change it to 'Foo(int)'.

I think you are just getting fooled by the 'varargs' nature of WriteLine.

你的心境我的脸 2024-07-20 03:01:36

不要依赖 WriteLine 告诉您 *y 和 y 是什么,从调试器运行它并亲自检查 *y 和 y 以查看差异。

Don't rely on WriteLine to tell you what *y and y are, run it from the debugger and inspect *y and y yourself to see the difference.

山有枢 2024-07-20 03:01:36

将值类型的句柄视为您自己编写的智能句柄,它具有用于转换的运算符重载。 这样,使用可以提供 int 的句柄(通过转换运算符)以及取消引用句柄以获取对象中包含的 int 都可以。

关于 Writeline,我认为它就像流输出运算符,其中隐式调用许多转换。 就像您可以安全地外出一样<< y,您可以编写 System::Console::WriteLine(y)。

Think of handles to value types as being like a smart handle you might have written yourself, which has operator overloading for conversions. That way, using the handle where you can supply an int would work (via the conversion operator) as well as dereferencing the handle to get the int contained in the object.

With regards to Writeline, I think of it as being like the stream output operators where many conversions are implicitly invoked. Just as you might safely go cout << y, you can write System::Console::WriteLine(y).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文