- /*
- (C) EnzoYang 2008
- Filename : testoverride.cpp
- Compiler : Visual C++ 9.0 / Visual Studio 2008
- Description : test about C++/CLI override
- Release : 11/30/2008 1.0
- */
- #include "stdafx.h"
- using namespace System;
- ref class B abstract
- {
- public:
- virtual void fun() abstract;
- virtual void f() abstract;
- virtual void error() {System::Console::WriteLine(L"myerror");};
- };
- interface class IB
- {
- void fun();
- };
- ref class D: B, IB
- {
- public:
- virtual void fun1() = B::fun //重写了B::fun
- {
- System::Console::WriteLine(L"b::fun");
- };
- virtual void fun2() = IB::fun //重写了IB::fun
- {
- System::Console::WriteLine(L"ib::fun");
- };
- virtual void f() override
- {
- System::Console::WriteLine(L"f");
- };
- virtual void fun() new //与上面的重写无冲突
- {
- System::Console::WriteLine(L"hello");
- };
- virtual void error() new = B::error //正确 但是error依然是重写了B::error
- {
- System::Console::WriteLine(L"error");
- };
- //***1***virtual void error() new { System::Console::WriteLine(L"error");};
- //正确 error只是隐藏了 B::error
- //virtual void error() override = B::error { System::Console::WriteLine(L"error");};
- //错误 ,error 不能被多次重写,这里显式和隐式各一次了
- //virtual void error() override = B::error { System::Console::WriteLine(L"error");};
- //错误同上
- };
- int main(array<System::String ^> ^args)
- {
- //String sstr;
- D^ d = gcnew D;
- d->fun();
- d->fun1();
- d->fun2();
- d->f();
- B^ b = d;
- b->fun();
- b->error(); // 这里的输出是error 如果用注释中的***1***那条语句的话输出时myerror
- IB^ ib = d;
- ib->fun();
- String ^str = Console::ReadLine();
- return 0;
- }
输出结果
hello
b::fun
ib::fun
f
b::fun
error
ib::fun