C++/CLI学习
Each time we assign the same Object with a value type, a new boxing of the value occurs.
Allowing access to the boxed value type allows in-memory update, which may provide significant
performance ...
当我们每次分配同一的值类型对象时,都会发生一次装箱操作。允许访问已装箱的值类型在内存中,将能显著的提高程序性能。
A form of deep-copy semantics associated with the C++ copy constructor and copy assignment
operator; however, this could not be extended to value types.
将深拷贝语法关联到C++的拷贝构造函数和拷贝操作符;但是没有扩展到值类型。
The CLI has no notion of the class destructor for a reference type. So the destructor has to be
mapped into something else in the underlying implementation。
CLI中对引用类型没有析构函数的概念。因此在实现的时候析构函数不得不映射到其他函数上。
We still need a way to automate the invocation of the
destructor. A special stack-based notation for a reference type is supported; that is, one in which its
lifetime is associated within the scope of its declaration. Internally, the compiler transforms the
notation to allocate the reference object on the managed heap. With the termination of the scope,
the compiler inserts an invocation of the Dispose() method—the user-defined destructor. Reclamation
of the actual memory associated with the object remains under the control of the garbage collector.
我们仍然需要一种方法去自动调用析构函数。对于引用类型提供了一个特殊的栈标志,对象的生命周期被关联到它声明的代码段中。
普遍的编译器根据这个标志分配一个引用对象在托管堆上。在代码段结束时,编译器插入一个Dispose()调用-用户定义的析构函数。
回收该对象关联的内存仍然处于垃圾收集器的控制下。
ref class Wrapper {
Native *pn;
public:
// 构造即初始化
Wrapper( int val ) { pn = new Native( val ); }
// 自己释放非托管内存
~Wrapper(){ delete pn; }
void mfunc();
protected:
// 明确声明Finalize方法确保内存会被释放
! Wrapper() { delete pn; }
};
void f1()
{
// 使用正常的引用类型
Wrapper^ w1 = gcnew Wrapper( 1024 );
// 映射引用类型到生命周期
Wrapper w2( 2048 );
// 比较调用语法的不同
w1->mfunc(); w2.mfunc();
// w2 在这里被Dispose()释放
}
//
// ... 然后, w1 可能在这里被Finalize()释放
C++/CLI编译四种模式
1.Mixed mode: source-level mix of native and CTS types plus binary mix of native and CIL
object files. (Compiler switch: \clr.)
混合模式:将原生的型别与CTS型别在代码级和二进制文件级进行混合,通过\clr参数
2.Pure mode: source-level mix of native and CTS types. All compiled to CIL object files.
(Compiler switch: \clr:pure.)
纯模式:将原生的型别与CTS型别在代码级混合,并编译所有文件到CIL对象文件,通过\clr:pure参数
3.Native class can hold CTS types through a special wrapper class only
原生类只能通过一个特殊封装类来处理CTS型别
4.CTS classes can hold native types only as pointers
CTS类能够处理的原生型别只有指针类型。
C++/CLI的组成
1.Assemblies(装配器)
装配器是构建.Net分布式应用程序的核心。装配器是一个自我描述的集合存储于中间语言中,并负责分配应用程序需要的资源。装配件由四个部分组成:装配件元数据,类型元数据,微软中间语言代码和资源。
Private assemblies reside in the same directory as the application itself or in one of its child directories. Shared assemblies, on the other hand, are stored in the global assembly cache (GAC).
私有装配器位于应用程序同一目录或是它的子目录。共享装配器存储在全局装配件缓存中(GAC)。
可以通过反射技术获得装配器中的元数据信息。
2.Common Language Runtime(统一语言运行环境)
as managed objects in .NET do not have a fixed location,but you can overcome this with the pin_ptr<> keyword.
托管对象在.NET中没有固定的地址,不过你可以使用pin_ptr<>关键字来控制它。
The CLS is a minimum subset of the CTS that all languages must support to be.
CLS是CTS的最小子集,所有的.NET语言都需要支持它.
• Global methods and variables are not allowed.
不允许使用全局函数和变量
• The CLS does not impose case sensitivity, so make sure that all exposed types differ by more than their case.
CLS不区分大小写
• The only primitive types allowed are Byte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, IntPtr, and String.
CLS只包含以上的几种简单的数据类型
• Variable-length argument lists are not allowed. Use fixed-length arrays instead.
使用固定的数组来代替可变参数列表
• Pointers are not allowed.
不允许指针
• Class types must inherit from a CLS-compliant class. System::Object is CLS compliant.
类类型必须继承自CLS体系的类(这就是为什么所有.NET类都继承值System::Object的原因)
• Array elements must be CLS compliant.
数组元素必须是CLS体系中的元素