C++/CLI Generic::字典声明语法

发布于 2024-10-05 20:07:27 字数 774 浏览 1 评论 0原文

我对 Collections::Generic::Dictionary 的声明语法很好奇 C++/CLI 中的类。

通常我们在类中声明一个引用并初始化它:

public ref class CDemo {
    private: ClassA ^ m_InstanceA;

    // Why the absence of '^'.
    private: Dictionary<int, int> m_Dic;

    CDemo() : 
        m_InstanceA(gcnew ClassA()),   
        m_Dic(gcnew Dictionary<int, int>()) 
    {...}
};

有人可以解释一下为什么“^”应该不存在吗?

更重要的是,如果我使用上面的字典作为另一个字典的TValue, 我必须这样声明:

Dictionary<T, Dictionary<T, T>^ > m_Dic;  // A '^' in the TValue parameter, which is           
                                          // normal, but same question as above,
                                          // I don't have to declare m_Dic as ^ ?

谢谢。

I'm been curious about the declaration syntax of Collections::Generic::Dictionary
class in C++/CLI.

Normally we declare a reference in a class and initialize it:

public ref class CDemo {
    private: ClassA ^ m_InstanceA;

    // Why the absence of '^'.
    private: Dictionary<int, int> m_Dic;

    CDemo() : 
        m_InstanceA(gcnew ClassA()),   
        m_Dic(gcnew Dictionary<int, int>()) 
    {...}
};

Could someone explains please why should the '^' absent there?

What's more, if I were to use the dictionary above as a TValue of another dictionary,
I have to declare it like this:

Dictionary<T, Dictionary<T, T>^ > m_Dic;  // A '^' in the TValue parameter, which is           
                                          // normal, but same question as above,
                                          // I don't have to declare m_Dic as ^ ?

Thanks.

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

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

发布评论

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

评论(1

苦妄 2024-10-12 20:07:27

这不是 Dictionary 特有的。此语法是一种帮助将 C++ 语义映射到托管类型的方法。一般来说:

ref class A
{
   ReferenceType m_obj;
}; 

大致相当于

class A : IDisposable
{
  private ReferenceType m_obj;
  void Dispose() { m_obj.Dispose(); }
}

C# 中的 if ReferenceType 实现 IDisposable。完全可以这样写:

ref class A
{
   ReferenceType^ m_obj;
};

This does not have theimplicit IDisposable support.另一个区别是您可以从方法返回 ReferenceType^,仅普通 ReferenceType 不支持此功能。例如:

ref class A
{ 
   ReferenceType^ m_obj;
   ReferenceType^ GetIt() { return m_obj; }
};

将编译,

ref class A
{
   ReferenceType m_obj;
   ReferenceType GetIt() { return m_obj; }  // won't compile
   ReferenceType^ OtherGetIt() { return m_obj; } // neither will this
};

将自动(堆栈变量)脱糖提供类似的区别

      ReferenceType local;
      local.Stuff();

编译器

      try {
        ReferenceType^ local = gcnew ReferenceType();
        local->Stuff();
      } finally {
        delete local; // invokes Dispose() (~ReferenceType)
      }

,这些功能将熟悉的 RAII 习惯用法引入具有托管类型的 C++/CLI。

编辑:

是的,IDisposable 的 Dispose 方法类似于 C++ 析构函数。如果 ReferenceType 未实现 IDisposable (没有 dtor),并且它是唯一的成员,则 A 也不会实现 < code>IDisposable (没有隐式 dtor)。在 C++/CLI 中,您可以通过提供 dtor(对于托管类型)来实现 IDisposable。

This is not specific to Dictionary. This syntax is a way to help map C++ semantics onto managed types. In general:

ref class A
{
   ReferenceType m_obj;
}; 

is roughly equivalent to

class A : IDisposable
{
  private ReferenceType m_obj;
  void Dispose() { m_obj.Dispose(); }
}

in C# if ReferenceType implements IDisposable. It is perfectly possible to write

ref class A
{
   ReferenceType^ m_obj;
};

This does not have the implicit IDisposable support. The other difference is that you can return a ReferenceType^ from a method, this is not supported with just plain ReferenceType. For example:

ref class A
{ 
   ReferenceType^ m_obj;
   ReferenceType^ GetIt() { return m_obj; }
};

will compile,

ref class A
{
   ReferenceType m_obj;
   ReferenceType GetIt() { return m_obj; }  // won't compile
   ReferenceType^ OtherGetIt() { return m_obj; } // neither will this
};

A similar distinction is provided for automatic (stack variables)

      ReferenceType local;
      local.Stuff();

is desugared by the compiler to

      try {
        ReferenceType^ local = gcnew ReferenceType();
        local->Stuff();
      } finally {
        delete local; // invokes Dispose() (~ReferenceType)
      }

These features bring the familiar idiom of RAII to C++/CLI with managed types.

EDIT:

Yes, the Dispose method of IDisposable is analogous to a C++ destructor. If ReferenceType doesn't implement IDisposable (doesn't have a dtor), and it is the only member, A will also not implement IDisposable (not have an implicit dtor). In C++/CLI you implement IDisposable by providing a dtor (for managed types).

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