Gcroot mẫu có một số thành viên cho phép bạn truy cập vào các đối tượng trong một cách thuận tiện. Nó thường có thể điều trị một biến gcroot loại như thể nó là của ^ FileSystemWatcher loại. Ví dụ, gcroot có một nhà xây dựng dự kiến một tham số của ^ FileSystemWatcher loại. | CHAPTER 8 MIXING THE MANAGED AND THE NATIVE TYPE SYSTEM 191 The template gcroot has several members that allow you to access the object in a convenient way. It is often possible to treat a variable of type gcroot FileSystemWatcherA as if it were of type FileSystemWatcherA. For example gcroot FileSystemWatcherA has a constructor that expects a parameter of type FileSystemWatcherA. This allows you to initialize a gcroot FileSystemWatcherA variable with a FileSystemWatcherA handle. The following code uses this constructor to implement the constructor of the class ChangedFileDumper ChangedFileDumper ChangedFileDumper std string path fsw gcnew FileSystemWatcher fsw- Path gcnew String In the body of the ChangedFileDumper the gcroot FileSystemWatcherA data member is used as if it were of type FileSystemWatcherA. Notice that the Path property is a property of FileSystemWatcher not a property of gcroot. This behavior is possible because the gcroot template overloads the member access operator operator- so that it returns the wrapped object. The gcroot template also implements a conversion operator that can be used to obtain the wrapped tracking handle. This operator can be used to dispose the FileSystemWatcher in the ChangedFileDumper destructor ChangedFileDumper delete FileSystemWatcherA fsw As an alternative you can also use a template called msclr auto_gcroot. As you can conclude from its name this template combines gcroot and auto_handle. On the one hand it allows a data member of a native type to refer to a managed object. On the other hand its destructor ensures that the wrapped managed object is disposed. To use msclr auto_gcroot you must include the header file msclr . Since FileSystemWatcher implements IDisposable it makes sense to use auto_gcroot instead of gcroot for the ChangedFileDumper class. The following code shows the complete application build with cl clr include string include include