您的位置:首页 > 编程语言 > C语言/C++

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

2006-07-04 15:45 666 查看
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;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐