1 // This file illustrates the cross language polymorphism using directors.
5 // CEO class, which overrides Employee::getPosition().
8 public CEO(String name) : base(name) {
10 public override String getPosition() {
13 // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
14 public void disownMemory() {
24 // Create an instance of CEO, a class derived from the C# proxy of the
25 // underlying C++ class. The calls to getName() and getPosition() are standard,
26 // the call to getTitle() uses the director wrappers to call CEO.getPosition().
28 CEO e = new CEO("Alice");
29 Console.WriteLine( e.getName() + " is a " + e.getPosition() );
30 Console.WriteLine( "Just call her \"" + e.getTitle() + "\"" );
31 Console.WriteLine( "----------------------" );
33 // Create a new EmployeeList instance. This class does not have a C++
34 // director wrapper, but can be used freely with other classes that do.
36 using (EmployeeList list = new EmployeeList()) {
38 // EmployeeList owns its items, so we must surrender ownership of objects we add.
41 Console.WriteLine( "----------------------" );
43 // Now we access the first four items in list (three are C++ objects that
44 // EmployeeList's constructor adds, the last is our CEO). The virtual
45 // methods of all these instances are treated the same. For items 0, 1, and
46 // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
47 // getPosition which resolves in C#. The call to getPosition is
48 // slightly different, however, because of the overidden getPosition() call, since
49 // now the object reference has been "laundered" by passing through
50 // EmployeeList as an Employee*. Previously, C# resolved the call
51 // immediately in CEO, but now C# thinks the object is an instance of
52 // class Employee. So the call passes through the
53 // Employee proxy class and on to the C wrappers and C++ director,
54 // eventually ending up back at the C# CEO implementation of getPosition().
55 // The call to getTitle() for item 3 runs the C++ Employee::getTitle()
56 // method, which in turn calls getPosition(). This virtual method call
57 // passes down through the C++ director class to the C# implementation
58 // in CEO. All this routing takes place transparently.
60 Console.WriteLine( "(position, title) for items 0-3:" );
62 Console.WriteLine( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" );
63 Console.WriteLine( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" );
64 Console.WriteLine( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" );
65 Console.WriteLine( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" );
66 Console.WriteLine( "----------------------" );
68 // The using statement ensures the EmployeeList.Dispose() will be called, which will delete all the Employee*
69 // items it contains. The last item is our CEO, which gets destroyed as well.
71 Console.WriteLine( "----------------------" );
75 Console.WriteLine( "C# exit" );