源文 :https://msdn.microsoft.com/zh-cn/library/ms235298%28v=vs.100%29.aspx

Visual Studio 2010
其他版本

此概要给出一些示例,说明 C++ 托管扩展与 Visual C++ 2010 语言的某些不同。 有关更多信息,请访问每个项附带的链接。

移除了所有关键字前面的双下划线,仅有一个例外。 这样,__value 变为 value,而 __interface 变为 interface 等等。 若要防止用户代码中关键字与标识符之间的冲突,请首先将关键字视为上下文。

有关更多信息,请参见语言关键字

托管扩展语法:

 
 
public __gc class Form1 : public System::Windows::Forms::Form {
private:
System::ComponentModel::Container __gc *components;
System::Windows::Forms::Button __gc *button1;
System::Windows::Forms::DataGrid __gc *myDataGrid;
System::Data::DataSet __gc *myDataSet;
};

新语法:

 
 
public ref class Form1 : System::Windows::Forms::Form {
System::ComponentModel::Container^ components;
System::Windows::Forms::Button^ button1;
System::Windows::Forms::DataGrid^ myDataGrid;
System::Data::DataSet^ myDataSet;
};

有关更多信息,请参见 CLR 引用类对象的声明

托管堆分配

托管扩展语法:

 
 
Button* button1 = new Button; // managed heap
int *pi1 = new int; // native heap
Int32 *pi2 = new Int32; // managed heap

新语法:

 
 
Button^ button1 = gcnew Button;        // managed heap
int * pi1 = new int; // native heap
Int32^ pi2 = gcnew Int32; // managed heap

有关更多信息,请参见 CLR 引用类对象的声明

对不存在的对象的跟踪引用

托管扩展语法:

 
 
// OK: we set obj to refer to no object
Object * obj = 0; // Error: no implicit boxing
Object * obj2 = 1;

新语法:

 
 
// Incorrect Translation
// causes the implicit boxing of both 0 and 1
Object ^ obj = 0;
Object ^ obj2 = 1; // Correct Translation
// OK: we set obj to refer to no object
Object ^ obj = nullptr; // OK: we initialize obj2 to an Int32^
Object ^ obj2 = 1;

有关更多信息,请参见 CLR 引用类对象的声明

CLR 数组经过了重新设计。 它类似于 stl vector 模板集合,但映射到基础 System::Array 类 — 也就是说,它不是模板实现。

有关更多信息,请参见 CLR 数组的声明

数组作为参数

托管扩展数组语法:

 
 
void PrintValues( Object* myArr __gc[]);
void PrintValues( int myArr __gc[,,]);

新数组语法:

 
 
void PrintValues( array<Object^>^ myArr );
void PrintValues( array<int,3>^ myArr );

数组作为返回类型

托管扩展数组语法:

 
 
Int32 f() [];
int GetArray() __gc[];

新数组语法:

 
 
array<Int32>^ f();
array<int>^ GetArray();

局部 CLR 数组的简略初始化

托管扩展数组语法:

 
 
int GetArray() __gc[] {
int a1 __gc[] = { 1, 2, 3, 4, 5 };
Object* myObjArray __gc[] = { __box(26), __box(27), __box(28),
__box(29), __box(30) }; return a1;
}

新数组语法:

 
 
array<int>^ GetArray() {
array<int>^ a1 = {1,2,3,4,5};
array<Object^>^ myObjArray = {26,27,28,29,30}; return a1;
}

显式 CLR 数组声明

托管扩展数组语法:

 
 
Object* myArray[] = new Object*[2];
String* myMat[,] = new String*[4,4];

新数组语法:

 
 
array<Object^>^ myArray = gcnew array<Object^>(2);
array<String^,2>^ myMat = gcnew array<String^,2>(4,4);

语言的新功能:显式数组初始化遵循 gcnew

 
 
// explicit initialization list follow gcnew
// is not supported in Managed Extensions
array<Object^>^ myArray =
gcnew array<Object^>(4){ 1, 1, 2, 3 };

托管扩展属性语法:

 
 
public __gc __sealed class Vector {
double _x; public:
__property double get_x(){ return _x; }
__property void set_x( double newx ){ _x = newx; }
};

新属性语法:

 
 
public ref class Vector sealed {
double _x; public:
property double x
{
double get() { return _x; }
void set( double newx ){ _x = newx; }
} // Note: no semi-colon …
};

语言的新功能:trivial 属性

 
 
public ref class Vector sealed {
public:
// equivalent shorthand property syntax
// backing store is not accessible
property double x;
};

有关更多信息,请参见属性声明

托管扩展索引属性语法:

 
 
public __gc class Matrix {
float mat[,]; public:
__property void set_Item( int r, int c, float value) { mat[r,c] = value; }
__property int get_Item( int r, int c ) { return mat[r,c]; }
};

新索引属性语法:

 
 
public ref class Matrix {
array<float, 2>^ mat; public:
property float Item [int,int] {
float get( int r, int c ) { return mat[r,c]; }
void set( int r, int c, float value ) { mat[r,c] = value; }
}
};

语言的新功能:类级索引属性

 
 
public ref class Matrix {
array<float, 2>^ mat; public:
// ok: class level indexer now
// Matrix mat;
// mat[ 0, 0 ] = 1;
//
// invokes the set accessor of the default indexer property float default [int,int] {
float get( int r, int c ) { return mat[r,c]; }
void set( int r, int c, float value ) { mat[r,c] = value; }
}
};

有关更多信息,请参见属性索引声明

托管扩展运算符重载语法:

 
 
public __gc __sealed class Vector {
public:
Vector( double x, double y, double z ); static bool op_Equality( const Vector*, const Vector* );
static Vector* op_Division( const Vector*, double );
}; int main() {
Vector *pa = new Vector( 0.231, 2.4745, 0.023 );
Vector *pb = new Vector( 1.475, 4.8916, -1.23 ); Vector *pc = Vector::op_Division( pa, 4.8916 ); if ( Vector::op_Equality( pa, pc ))
;
}

新运算符重载语法:

 
 
public ref class Vector sealed {
public:
Vector( double x, double y, double z ); static bool operator ==( const Vector^, const Vector^ );
static Vector^ operator /( const Vector^, double );
}; int main() {
Vector^ pa = gcnew Vector( 0.231, 2.4745, 0.023 );
Vector^ pb = gcnew Vector( 1.475, 4.8916, -1.23 ); Vector^ pc = pa / 4.8916;
if ( pc == pa )
;
}

有关更多信息,请参见 重载运算符

托管扩展转换运算符语法:

 
 
__gc struct MyDouble {
static MyDouble* op_Implicit( int i );
static int op_Explicit( MyDouble* val );
static String* op_Explicit( MyDouble* val );
};

新转换运算符语法:

 
 
ref struct MyDouble {
public:
static operator MyDouble^ ( int i );
static explicit operator int ( MyDouble^ val );
static explicit operator String^ ( MyDouble^ val );
};

有关更多信息,请参见转换运算符的更改

托管扩展显式重写语法:

 
 
public __gc class R : public ICloneable {
// to be used through ICloneable
Object* ICloneable::Clone(); // to be used through an R
R* Clone();
};

新显式重写语法:

 
 
public ref class R : public ICloneable {
// to be used through ICloneable
virtual Object^ InterfaceClone() = ICloneable::Clone; // to be used through an R
virtual R^ Clone();
};

有关更多信息,请参见接口成员的显式重写

托管扩展私有虚函数语法:

 
 
__gc class Base {
private:
// inaccessible to a derived class
virtual void g();
}; __gc class Derived : public Base {
public:
// ok: g() overrides Base::g()
virtual void g();
};

新私有虚函数语法

 
 
ref class Base {
private:
// inaccessible to a derived class
virtual void g();
}; ref class Derived : public Base {
public:
// error: cannot override: Base::g() is inaccessible
virtual void g() override;
};

有关更多信息,请参见私有虚函数

托管扩展枚举语法:

 
 
__value enum e1 { fail, pass };
public __value enum e2 : unsigned short {
not_ok = 1024,
maybe, ok = 2048
};

新枚举语法:

 
 
enum class e1 { fail, pass };
public enum class e2 : unsigned short {
not_ok = 1024,
maybe, ok = 2048
};

除此语法的稍微更改外,CLR 枚举的行为在许多方面发生了更改:

  • 不再支持 CLR 枚举的前向声明。

  • 内置算术类型和对象类层次结构之间的重载决策在托管扩展和 Visual C++ 2010 之间是相反的。 它的副作用是 CLR 枚举不再隐式转换为算术类型。

  • 在新的语法中,CLR 枚举保持其自身范围,而在托管扩展中则不是这样。 以前,枚举数在枚举的包含范围内可见;现在,枚举数被封装在枚举的范围内。

有关更多信息,请参见 CLR 枚举类型

托管扩展装箱语法:

 
 
Object *o = __box( 1024 ); // explicit boxing

新装箱语法:

 
 
Object ^o = 1024; // implicit boxing

有关更多信息,请参见装箱值的跟踪句柄

托管扩展钉住指针语法:

 
 
__gc struct H { int j; };

int main() {
H * h = new H;
int __pin * k = & h -> j;
};

新的钉住指针语法:

 
 
ref struct H { int j; };

int main() {
H^ h = gcnew H;
pin_ptr<int> k = &h->j;
}

有关更多信息,请参见值类型语义

托管扩展 typeof 语法:

 
 
Array* myIntArray =
Array::CreateInstance( __typeof(Int32), 5 );

新 typeid 语法:

 
 
Array^ myIntArray =
Array::CreateInstance( Int32::typeid, 5 );

有关更多信息,请参见 typeof 转到 T::typeid

C++/C# 托管扩展 更改概要 [转]的更多相关文章

  1. C# using 三种使用方式 C#中托管与非托管 C#托管资源和非托管资源区别

    1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...

  2. oracle_用户与概要文件

    Oracle 用户与概要文件 2012-09-01 15:05:47| 分类: Oracle | 标签:用户与概要文件 |举报 |字号大中小 订阅     用户管理看上去简单其实也是最常出现问题的一个 ...

  3. 【Eclipse】更改部署位置

    在使用eclipse启动tomcat时,偶尔会遇到应用没被部署的现象,导致访问时出现404 问题原因:应用部署在了eclipse自带的webapps中. 我们通常不喜欢eclipse自带的tomcat ...

  4. 托管C++、C++/CLI、CLR

    1.什么是托管C++? 在回答这个问题,首先要搞清楚什么是"托管"(Managed).托管是.NET的一个专门概念,它是融于通用语言运行时(CLR)中的一种新的编程理念,因此我们完 ...

  5. C#的托管与非托管大难点

    托管代码与非托管代码 众所周知,我们正常编程所用的高级语言,是无法被计算机识别的.需要先将高级语言翻译为机器语言,才能被机器理解和运行.在标准C/C++中,编译过程是这样的:源代码首先经过预处理器,对 ...

  6. .NET MVC5+ Dapper+扩展+AutoFac自动注入实现

    1.首先创建一个MVC项目 定义Model 层  view 层 index.cshtml  控制器层Controllers等文件 2.在线安装或者引用dapper 以及扩展相关包 同时Autofac ...

  7. 如何删除Struts2动作的后缀扩展名

    在Struts2中,所有动作类有一个默认的后缀 .action 扩展. 例如, <struts> <package name="default" namespac ...

  8. C#的三大难点之二:托管与非托管

    相关文章: C#的三大难点之前传:什么时候应该使用C#?​C#的三大难点之一:byte与char,string与StringBuilderC#的三大难点之二:托管与非托管C#的三大难点之三:消息与事件 ...

  9. ASP.NET 网站迁移 HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用

    前提:今天要做网站迁移,把A服务器上的网站迁移到B服务器上,A服务器当时的环境是.NET 4.0 ,而B服务器是.NET2.0,A服务器IIS为6.0,B服务器IIS版本为7.0  第一步,先在B服务 ...

随机推荐

  1. [oldboy-django][2深入django]老师管理--查看,添加,编辑

    # 添加老师(下拉框多选) 数据库设计: class Teacher(models.Model): name = models.CharField(max_length=64) cls = model ...

  2. 【homework week5】初步了解敏捷开发——自由与约束的哲学统一

    “自由与束缚的哲学统一”或许不该放到标题上去,毕竟它只是我灵光一闪的感悟.但这个spark让我感到高中到大学的哲学应该也没有白学,这是让人非常兴奋的一件事. 所以我还是把它放到了标题上. 来谈敏捷软件 ...

  3. 设计模式 uml元素

    uml的构造包含3种 事物4种:结构,行为,分组,注释事物 关系4种:泛化,实现,依赖,关联, 图10种,用例图,类图,对象,包,组件,部署,状态,活动,序列,协作 事物是对模型中最具代表性的成分的抽 ...

  4. springmvc支持跨域的请求(复制)

    Spring MVC 新增跨域支持 发表于2017/5/8 22:01:24  48人阅读 分类: SpringMVC Spring MVC 4.2 增加 CORS 支持 跨站 HTTP 请求(Cro ...

  5. Unity 碰撞检测

    武器与怪物的碰撞 目前来说有三种思路,其实前两种算变种了: 1.动画关键帧回调 + 范围检测.http://blog.csdn.net/u013700908/article/details/52888 ...

  6. POJ 3368:Frequent values(线段树区间合并)

    题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...

  7. Dos中查看mysql数据时 中文乱码

    使用jsp页面查看数据时可以正确显示中文,但是dos窗口查看数据时中文显示乱码. 上网查了一下原因:之所以会显示乱码,就是因为MySQL客户端输出窗口显示中文时使用的字符编码不对造成的,可以使用如下的 ...

  8. "二进制" 转化为 "十六进制

    //"二进制" 转化为 "十六进制" void To_string(uint8 *dest,char * src,uint8 length) { uint8 * ...

  9. sqlmap post注入两种方式

    1. 使用抓包工具抓取数据包,sqlmap加载数据包 POST /SME/static/orderFind/orderIntfaceph HTTP/1.1 Host: abc.com User-Age ...

  10. 将扁平化的JSON属性转换为嵌套的JSON

    需要将如下JSON {"a":"a","b":"b","c.e":"e",&qu ...