<@乌龟:>C++/CLI语言Specification阅读笔记(1)
2009-07-29 16:34
411 查看
本笔记主要包含以下的内容:
1.如何在VC++2005或者2008中打开CLR支持
2.一个简单的Hello World程序
3.引用类型与值类型
4.常见CLI类型的定义
5.CLI中的数组(CLI::array)
6.统一类型的系统(System Unification)
1.如何在VC++2005或者2008中打开CLR支持
在当前的解决方案浏览器中选择解决方案,按右键,选择Property,在弹出的对话框的Configuration Properties的General菜单下选择Common Language Runtime Support中选择Common Language Runtime Support (/clr)选项,就可以正常使用.Net的内容了
2.一个简单的Hello World程序
using namespace System
int main()
{
System::Console::WriteLine("hello, world");
}
3.引用类型与值类型
using namespace System;
ref class Class1
{
public:
int value;
Class1()
{
value = 0;
}
};
int main()
{
int val1 = 0;
int val2 = val1;
val2 = 123;
Class1^ ref1 = gcnew Class1;
Class1^ ref2 = ref1;
ref2->value = 123;
Console::WriteLine("Values: {0},{1}",val1,val2);
Console::WriteLine("Refs: {0},{1}",ref1->value,ref2->value);
}
注:
1) ^符号表示该类型为托管类型
2)gcnew与new的区别在与,gcnew是在托管堆上创建对象,可以实现自动的垃圾回收
3)ref代表该类型为托管的引用类型.
4.常见CLI类型的定义
public enum class Color
{
Red, Blue, Green
};
public value struct Point
{
int x, y;
};
public interface class IBase
{
void F();
};
public interface class IDerived: IBase
{
void G();
};
public ref class A
{
protected:
virtual void H()
{
Console::WriteLine("A.H");
}
};
public ref class B : A, IDerived
{
public:
virtual void F()
{
}
virtual void G()
{
}
protected:
virtual void H() override
{
}
};
public delegate void MyDelegate();
包含了常见的类型的定义
5.CLI中的数组(CLI:array)
using namespace System;
int main()
{
array<int>^ arr1D = gcnew array<int>(4){10,42,30,12};
Console::Write("The {0} elements are:", arr1D->Length);
for (int i = 0; i < arr1D.Length; i ++)
{
Console::Write("{0,3}", i);
}
Console::WriteLine();
array<int, 3>^ arr3D = gcnew array<int, 3>(10,20,30);
}
注:
1)array<int>^表示这是托管的数组
2)array可以是多维的数组,维度在array的建立的时候指定,如arr3D就是一个10*20*30的数组
6.统一类型的系统
using namespace System;
int main()
{
Console::WriteLine((3).ToString());
int i = 123;
Object^ o = i; //boxing
int j = safe_cast<int>(o); //unboxing
Console::WriteLine("{0}",j);
}
注:
在java或者c#中,都是采用同一类型的系统,即所有的类型都是集成自Object类型.而传统的C++不是这样,在CLI中,所有的类型都是集成自System::Object.
在上面程序中,数字3也具有ToString()方法.
另外一点就是装箱和拆箱的操作,在CLI中可以直接把值类型赋值给引用类型.叫做装箱操作,同样,调用safe_cast方法可以完成引用类型到值类型的转换,成为装箱操作.这些操作都是比较费时的
转载请注明出处,如果有问题欢迎指教
1.如何在VC++2005或者2008中打开CLR支持
2.一个简单的Hello World程序
3.引用类型与值类型
4.常见CLI类型的定义
5.CLI中的数组(CLI::array)
6.统一类型的系统(System Unification)
1.如何在VC++2005或者2008中打开CLR支持
在当前的解决方案浏览器中选择解决方案,按右键,选择Property,在弹出的对话框的Configuration Properties的General菜单下选择Common Language Runtime Support中选择Common Language Runtime Support (/clr)选项,就可以正常使用.Net的内容了
2.一个简单的Hello World程序
using namespace System
int main()
{
System::Console::WriteLine("hello, world");
}
3.引用类型与值类型
using namespace System;
ref class Class1
{
public:
int value;
Class1()
{
value = 0;
}
};
int main()
{
int val1 = 0;
int val2 = val1;
val2 = 123;
Class1^ ref1 = gcnew Class1;
Class1^ ref2 = ref1;
ref2->value = 123;
Console::WriteLine("Values: {0},{1}",val1,val2);
Console::WriteLine("Refs: {0},{1}",ref1->value,ref2->value);
}
注:
1) ^符号表示该类型为托管类型
2)gcnew与new的区别在与,gcnew是在托管堆上创建对象,可以实现自动的垃圾回收
3)ref代表该类型为托管的引用类型.
4.常见CLI类型的定义
public enum class Color
{
Red, Blue, Green
};
public value struct Point
{
int x, y;
};
public interface class IBase
{
void F();
};
public interface class IDerived: IBase
{
void G();
};
public ref class A
{
protected:
virtual void H()
{
Console::WriteLine("A.H");
}
};
public ref class B : A, IDerived
{
public:
virtual void F()
{
}
virtual void G()
{
}
protected:
virtual void H() override
{
}
};
public delegate void MyDelegate();
包含了常见的类型的定义
5.CLI中的数组(CLI:array)
using namespace System;
int main()
{
array<int>^ arr1D = gcnew array<int>(4){10,42,30,12};
Console::Write("The {0} elements are:", arr1D->Length);
for (int i = 0; i < arr1D.Length; i ++)
{
Console::Write("{0,3}", i);
}
Console::WriteLine();
array<int, 3>^ arr3D = gcnew array<int, 3>(10,20,30);
}
注:
1)array<int>^表示这是托管的数组
2)array可以是多维的数组,维度在array的建立的时候指定,如arr3D就是一个10*20*30的数组
6.统一类型的系统
using namespace System;
int main()
{
Console::WriteLine((3).ToString());
int i = 123;
Object^ o = i; //boxing
int j = safe_cast<int>(o); //unboxing
Console::WriteLine("{0}",j);
}
注:
在java或者c#中,都是采用同一类型的系统,即所有的类型都是集成自Object类型.而传统的C++不是这样,在CLI中,所有的类型都是集成自System::Object.
在上面程序中,数字3也具有ToString()方法.
另外一点就是装箱和拆箱的操作,在CLI中可以直接把值类型赋值给引用类型.叫做装箱操作,同样,调用safe_cast方法可以完成引用类型到值类型的转换,成为装箱操作.这些操作都是比较费时的
转载请注明出处,如果有问题欢迎指教
相关文章推荐
- <@乌龟:>C++/CLI语言Specification阅读笔记(2)
- <C++Primer>第四版 阅读笔记 第三部分 “类和数据抽象”
- <C++Primer>第四版 阅读笔记 第一部分 “基本语言”
- <C++Primer>第四版 阅读笔记 第四部分 “面向对象编程与泛型编程”
- <C++Primer>第四版 阅读笔记 第一部分 “基本语言”
- <<浪潮之巅>>阅读笔记三
- <疯狂Android讲义>阅读笔记(4)
- <@乌龟:>从c#到c++的一些容易出现的困惑(2)
- <Effective Mordern C++>笔记:Item 8:prefer nullptr to 0 and NULL.
- <Windows系统>使用C/C++语言控制7.1声道音量源代码
- <<JavaScript编程全解>>阅读笔记之变量与对象
- <More Effective C++>笔记--技巧
- <<重构手册>>阅读笔记:重构的含义
- <C++Primer>第四版 阅读笔记 第二部分 “容器和算法”
- <<深度探索linux操作系统>> 阅读笔记一
- <<梦断代码>>阅读笔记一
- <SLAM for Dummies>阅读笔记
- <转> amazon的一个大牛谈各种语言 c c++ java python ruby lisp perl
- <C++学习笔记>iterator C++
- <疯狂Andorid讲义> 阅读笔记(1)