Pro Visual C++/CLI and the .NET 2.0 Platform - read book log I.

 

1.         Implicit Virtual Overriding

For implicit overriding, the method signature of the base ref class must be the same as the derived

ref class including the prefix virtual. This means that the name of the method and the number of

parameters and their types must be identical. The return type of the method need not be identical,

but it must at least be derived from the same type as that of the base method’s return type. Also, you

need to append the new keyword override after the parameters:

virtual void Speak () override

{

}

 

2.          Explicit or Named Virtual Overriding

Explicit or named overriding allows you to assign a method with a different name to a virtual function.

To do this, you need to declare the overriding method as virtual and then assign the name of the

virtual method being overridden:

ref class Puppy : public Dog

{

public:

virtual void Yip () = Dog::Speak

{

}

};

 

3.         Operators

Not all operators can be overloaded. The most notable missing operators are the open and

closed square brackets [], open and closed round brackets (), gcnew, new, and delete. The Table 3-1

is a list of the operators available to be overloaded.

There are two types of operator overloads: unary and binary. You would have a good case, if you

claimed that the increment and decrement operators are a third type of operator. As you will see,

Table 3-1. Supported Managed Operators

Operators

+ - * / %

^ & | ~ !

= < > += -=

*= /= %= ^= &=

|= << >> <<= >>=

== != <= >= &&

|| ++ -- ,

 

4.         Static Properties

As I mentioned previously, ref classes also contain static member variables. Likewise, C++/CLI

provides property syntax to support static properties, or properties that have ref class-wide storage.

Static properties are nearly identical to scalar properties except that they contain the keyword

static in their definition and they can only use static variables for storage. To create a readable and

writable static property, simply use this syntax:

property static type PropertyName

{

type get() {};

void set (type value) {};

}

 

5.        Indexed Properties

At first glance, indexed properties may appear to provide the same functionality as array properties.

They allow you to look up a property based on an index. The syntax to allow you to do this is more

complex than that of the array property:

property type PropertyName [ indexType1, ..., indexTypeN ]

{

type get(indexType1 index1, ..., indexTypeN indexN) {};

void set(indexType1 index1, ..., indexTypeN indexN, type value) {};

}

6.        Default Indexed Property

property String^ default [int]

{

String^ get(int index)

{

if (index < 0)

index = 0;

else if (index > defaultArray->Length)

index = defaultArray->Length - 1;

return defaultArray[index];

}

}

 

7.         C++/CLI provides three different operators for type casting between classes or structs:

static_cast, dynamic_cast, and safe_cast. Each performs the process of trying to convert from

one class type to another.

l         The static_cast operator cannot be verified and thus is classified as unsafe code.

l         The dynamic_cast operator is slower than the static_cast operator because it verifies that the

type casting is valid. If the conversion is allowed, then the dynamic_cast operator completes the

conversion. On the other hand, if it’s not a valid conversion, then the dynamic_cast operator returns

nullptr.

if ( dynamic_cast<ClassA^>(ClassB) != 0)

{

// ClassB is of type ClassA

}

 

l          

8.        Abstract class

ref class AbstractExClass abstract

{

virtual void Method2() = 0; // unimplemented method

 

};

 

9.         interface class Interface2

{

void Method3();

property String^ X;

};

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值