继承
C++/cli中的继承用法基本和C#中的用法相同,只不过要注意以下几点:
- 标准的C++在继承符号
:
之后,基类名称之前添加关键字public/protected/private
,但是C++/CLI只支持public,所以可写可不写 - 标准C++声明抽象基类的方式是在内部至少将一个虚函数=0,设置为纯虚函数,而C++/CLI中则是在类声明后面加上abstract关键字
- 抽象函数在C++中叫做纯虚函数,也就是在虚函数后面=0,C++/CLI也支持这样做,另外第二种方式是可以在虚函数后面加上abstract关键字来表示为抽象函数
- C++/Cli支持密封类,也就是不能被继承,用sealed关键字
ref class Myclass sealed
- C++/cli中抽象类和密封类可同时声明,sealed和abstact顺序任意
- C++/CLI不支持多继承,只能多继承接口,接口使用interface来声明
interface clas IXmlWriter
,且接口成员都是公共的和抽象的
案例演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| interface class ICanShout { void Shout(); };
ref class Animal abstract:ICanShout { public: Animal(String^ n,int l); virtual void Eat() abstract; virtual void Shout() =0; String^ name; int legs; };
ref class Dog:Animal { public: Dog(String^ n,int l); void Eat() override; virtual void Shout() override; };
ref class Cat :Animal { public: Cat(String^ n, int l); void Eat() override; virtual void Shout() override; };
Animal::Animal(String^ n, int l) { this->name = n; this->legs = l; }
Dog::Dog(String^ n, int l):Animal(n,l) { }
void Dog::Eat() { Console::WriteLine("我的名字是{0},我吃骨头", name); }
void Dog::Shout() { Console::WriteLine("我会汪汪叫"); }
Cat::Cat(String^ n, int l):Animal(n,l) { }
void Cat::Eat() { Console::WriteLine("我的名字是{0},我吃鱼", name); }
void Cat::Shout() { Console::WriteLine("我会喵喵叫"); }
int main(array<System::String^>^ args) { Dog^ d = gcnew Dog("tom", 4); d->Eat(); d->Shout();
Cat^ c = gcnew Cat("jerry", 4); c->Eat(); c->Shout();
Animal^ a = d; a->Shout(); Console::WriteLine("程序结束"); }
|
值类型
值类型的特点:
- 存在栈上
- 不进行垃圾回收
- 总是直接访问,不使用gcnew
- 拷贝类类型是直接拷贝
- 不能继承
结构
1 2 3 4 5 6 7
| value struct Point { int x, y; };
|
结构和类的基本区别:
- 不能在结构定义时初始化成员,必须在构造器中初始化
- 不能重写结构默认构造器,因为默认构造器要将所有成员设为默认值
- 不能有析构和终结器
- 不支持继承
- 可实现接口
案例
1 2 3 4 5 6 7 8 9
| value struct Line { String^ name; Point p; };
Line l = { "线1",{3,4} }; Console::WriteLine(l.name);
|
枚举
1 2 3 4 5 6 7 8
|
public enum class WeekDay { Mondy,Tuesday };
Console::WriteLine(WeekDay::Mondy);
|
枚举虽然是整数值,但是不能隐式转换,必须这样int day = static_cast<int>(WeekDay::Tuesday);
enum默认是int大小是32位,但是一般1个字节就可以容纳所有的值,为了节省内存,可以增加限定public enum class WeekDay:char
操作符重载
重载操作符的方法和规范基本和C++重载操作符一致,请看下面案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| value struct IntVal { private: int value; public: IntVal(int v) :value(v) {}; int GetValue() { return value; } IntVal operator+(IntVal other) { IntVal result(value + other.GetValue()); return result; }
IntVal operator+(int other) { IntVal result(value + other); return result; }
static IntVal operator+(int lhs, IntVal rhs) { IntVal result(lhs + rhs.GetValue()); return result; } };
int main(array<System::String^>^ args) { IntVal one(1); IntVal two(2); IntVal three = one + two;
IntVal t = one + 2;
IntVal t = 2 + one ;
Console::WriteLine(t.GetValue()); Console::WriteLine("程序结束"); }
|
转换器
上面案例中仅仅实现了static IntVal operator+(int lhs, IntVal rhs)
,其实还需要实现(IntVal IntVal)\(IntVal,int)
其实还有个更简单的办法,也就是重载转换操作符(),将int转换为IntVal。与标准C++不同,在C++中如果定义了int构造器,编译器就允许将int隐式转为IntVal,但是在C++/CLI中必须重载转换操作符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| value struct IntVal { private: int value; public: IntVal(int v) :value(v) {}; int GetValue() { return value; } static operator IntVal(int v) { return IntVal(v); } static IntVal operator+(IntVal lhs, IntVal rhs) { IntVal result(lhs.value + rhs.value); return result; } };
|
**另外,与C++不同的是,C++中+并不能自动得到+=,但是在C++/CLI中,重载+会自动得到+=**,上面的重载函数完成后,可以实现
1 2
| IntVal one(1); one += 2;
|
递增和递减
标准C++要分别为前++和后++提供两个操作符重载,但是在C++/cli中只需要一个静态重载就可以,需注意要配合重载转换操作符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| value struct IntVal { private: int value; public: IntVal(int v) :value(v) {}; int GetValue() { return value; } static operator IntVal(int v) { return IntVal(v); } static IntVal operator++(IntVal i) { i.value++; return i; } };
int main(array<System::String^>^ args) { IntVal one1(1); IntVal one2(1);
Console::WriteLine((one1++).GetValue()); Console::WriteLine(one1.GetValue());
Console::WriteLine((++one2).GetValue()); Console::WriteLine(one2.GetValue());
Console::WriteLine("程序结束"); }
|

引用类型重载操作符
为引用类型重载操作符和为值类型类似,就是要关注对象句柄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| ref struct IntVal { private: int value; public: IntVal(int v) :value(v) {}; int GetValue() { return value; } static operator IntVal^(int v) { return gcnew IntVal(v); } static IntVal^ operator+(IntVal^ lhs,IntVal^ rhs) { IntVal^ result = gcnew IntVal(lhs->value + rhs->value); return result; } };
int main(array<System::String^>^ args) { IntVal^ one = gcnew IntVal(1); IntVal^ two = gcnew IntVal(2);
IntVal^ three = one + two; IntVal^ four = two + 2; Console::WriteLine(three->GetValue()); Console::WriteLine(four->GetValue());
Console::WriteLine("程序结束"); }
|

异常
C++/CLI中使用异常基本与C#中的使用方法相同。在动态类型转换上,safe_cast
转型失败会抛出InvalidCastException
,而dynamic_cast
则会返回空指针。