Syntax C++/CLI Generic::字典声明语法
我一直对Collections::Generic::Dictionary的声明语法感到好奇 在C++/CLI中初始化 通常,我们在类中声明引用并初始化它:Syntax C++/CLI Generic::字典声明语法,syntax,dictionary,c++-cli,declaration,Syntax,Dictionary,C++ Cli,Declaration,我一直对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_Instanc
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>())
{...}
};
public ref class CDemo{
私人:ClassA^m_InstanceA;
//为什么没有“^”呢。
私人:字典m_Dic;
CDemo():
m_InstanceA(gcnew ClassA()),
m_Dic(gcnew Dictionary())
{...}
};
有人能解释一下为什么“^”不在那里吗
更重要的是,如果我将上面的字典用作另一本字典的t值,
我必须这样声明:
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 ^ ?
字典m_Dic;//TValue参数中的“^”是
//正常,但问题与上述相同,
//我不必声明m_Dic为^?
谢谢。这不是
字典所特有的。此语法是帮助将C++语义映射到托管类型的方法。一般而言:
ref class A
{
ReferenceType m_obj;
};
大致相当于
class A : IDisposable
{
private ReferenceType m_obj;
void Dispose() { m_obj.Dispose(); }
}
在C#ifReferenceType
中实现了IDisposable
。写作是完全可能的
ref class A
{
ReferenceType^ m_obj;
};
这没有隐式的IDisposable
支持。另一个区别是,您可以从方法返回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
};
自动(堆栈变量)也有类似的区别
由编译器将其解压缩为
try {
ReferenceType^ local = gcnew ReferenceType();
local->Stuff();
} finally {
delete local; // invokes Dispose() (~ReferenceType)
}
这些特性将熟悉的RAII习惯用法带到具有托管类型的C++/CLI
编辑:
是的,IDISPOSIVEND的处置方法类似于C++析构函数。如果
ReferenceType
没有实现IDisposable
(没有dtor),并且它是唯一的成员,a
也不会实现IDisposable
(没有隐式dtor)。在C++/CLI中,可以通过提供dtor(用于托管类型)来实现IDisposable
。这不是特定于字典的。此语法是帮助将C++语义映射到托管类型的方法。一般而言:
ref class A
{
ReferenceType m_obj;
};
大致相当于
class A : IDisposable
{
private ReferenceType m_obj;
void Dispose() { m_obj.Dispose(); }
}
在C#ifReferenceType
中实现了IDisposable
。写作是完全可能的
ref class A
{
ReferenceType^ m_obj;
};
这没有隐式的IDisposable
支持。另一个区别是,您可以从方法返回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
};
自动(堆栈变量)也有类似的区别
由编译器将其解压缩为
try {
ReferenceType^ local = gcnew ReferenceType();
local->Stuff();
} finally {
delete local; // invokes Dispose() (~ReferenceType)
}
这些特性将熟悉的RAII习惯用法带到具有托管类型的C++/CLI
编辑:
是的,IDISPOSIVEND的处置方法类似于C++析构函数。如果
ReferenceType
没有实现IDisposable
(没有dtor),并且它是唯一的成员,a
也不会实现IDisposable
(没有隐式dtor)。在C++/CLI中,您通过提供dtor(用于托管类型)来实现IDisposable
。谢谢您的回答。你能再解释一下第二点吗?“在C#if ReferenceType中实现IDisposable。完全可以写入…”这里的IDisposable与C++/CLI的dtor相同?如果C#type没有实现IDisposable接口怎么办?谢谢你的回答。你能再解释一下第二点吗?“在C#if ReferenceType中实现IDisposable。完全可以写入…”这里的IDisposable与C++/CLI的dtor相同?如果C类型没有实现IDisposable接口怎么办?这是一个bug。查看为构造函数生成的IL,注意如何创建两个字典。这是一个bug。查看为构造函数生成的IL,注意如何创建两个字典。