home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / chap28 / hexadds / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  1.5 KB  |  88 lines

  1. unit Main;
  2.  
  3. { Program copyright (c) 1995 by Charles Calvert }
  4. { Project Name: HEXADDS }
  5.  
  6. { This program shows what happens to method addresses
  7.   in programs that implement polymorphism. }
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs,
  13.   Messages, Classes, Graphics,
  14.   Controls, Forms, Dialogs,
  15.   StdCtrls;
  16.  
  17. type
  18.   TForm1 = class(TForm)
  19.     PressMe: TButton;
  20.     Edit1: TEdit;
  21.     Edit2: TEdit;
  22.     ListBox1: TListBox;
  23.     Edit3: TEdit;
  24.     Label1: TLabel;
  25.     Label2: TLabel;
  26.     Label3: TLabel;
  27.     Label4: TLabel;
  28.     Label5: TLabel;
  29.     procedure PressMeClick(Sender: TObject);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36.   TParent = class(TEdit)
  37.   published
  38.     procedure Foo; virtual;
  39.   end;
  40.  
  41.   TChild = class(TParent)
  42.   published
  43.     procedure Foo; override;
  44.   end;
  45.  
  46. var
  47.   Form1: TForm1;
  48.  
  49. implementation
  50.  
  51. uses
  52.   StrBox;
  53.  
  54. {$R *.DFM}
  55.  
  56. procedure TParent.Foo;
  57. begin
  58.   Form1.ListBox1.Items.Add('ParentFoo called');
  59. end;
  60.  
  61. procedure TChild.Foo;
  62. begin
  63.   Form1.ListBox1.Items.Add('ChildFoo called');
  64. end;
  65.  
  66. procedure TForm1.PressMeClick(Sender: TObject);
  67. var
  68.   P: TParent;
  69.   C: TChild;
  70.   Ptr: Pointer;
  71. begin
  72.   Label1.Caption := GetHexWord(CSeg);
  73.   C := TChild.Create(Self);
  74.   Ptr := C.MethodAddress('Foo');
  75.   Edit1.Text := Address2Str(Ptr);
  76.   C.Foo;
  77.   P := C;
  78.   Edit2.Text := Address2Str(P.MethodAddress('Foo'));
  79.   P.Foo;
  80.   C.Free;
  81.   P := TParent.Create(Self);
  82.   P.Foo;
  83.   Edit3.Text := Address2Str(P.MethodAddress('Foo'));
  84.   P.Free;
  85. end;
  86.  
  87. end.
  88.