tailieunhanh - Expert C++/CLI .NET for Visual C++ Programmers phần 4

Để đảm bảo rằng sự mơ hồ ít nhất là phát hiện khi các phiên bản tiếp theo của các lớp học có nguồn gốc được xây dựng, trình biên dịch phát ra cảnh báo cấp độ 4. Bạn có thể sử dụng từ khoá mới để thể hiện rằng bạn nhận thức được rằng bạn đã xác định một chức năng không liên quan có cùng tên và chữ ký trong các lớp cơ sở. | CHAPTER 5 DEFINING MANAGED TYPES 91 To make sure that the ambiguity is at least detected when the next version of the derived class is built the compiler emits a level 4 warning. You can use the keyword new to express that you are aware that you have defined an unrelated function with the same name and signature as in the base class. This avoids the ambiguity warning. ref class Derived public Base public void g new Virtual Functions Extending a base class with virtual functions can cause even more problems. Some of these problems are in the nature of public inheritance other problems exist because the C syntax for virtual functions leaves room for ambiguities. The following code shows an ambiguity that exists in C code class Derived public Base public void f If you see just this class declaration you cannot say whether the function f is a virtual function or not. If Base defines or inherits a virtual function f with the same and signature as Derived f then Derived f overrides this virtual function and therefore Derived f is a virtual function too. This is sometimes called implicit overriding. When overriding a virtual function in C the keyword virtual is optional Derived f overrides Base f without explicitly stating this. To make clear that Derived f is a virtual function you could define the function with the keyword virtual. However in this case it would not be clear whether Derived overrides Base f or introduces a new virtual function f. C CLI introduces a new syntax for the declaration of virtual functions. This syntax is mandatory for virtual functions of managed types. If you compile with clr or clr pure you can also use this syntax for native classes. The new syntax is called explicit overriding. This means that the keyword virtual is no longer optional whenever you declare a virtual function you have to use the keyword virtual. This also means that the lack of this keyword is a sure indicator that the function is not virtual. In addition to the keyword .