home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / pascal / 7743 < prev    next >
Encoding:
Internet Message Format  |  1992-12-30  |  2.2 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!dtix!oasys!roth
  2. From: roth@oasys.dt.navy.mil (Pete Roth)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Polymorphic Poser
  5. Message-ID: <29121@oasys.dt.navy.mil>
  6. Date: 30 Dec 92 18:51:06 GMT
  7. Reply-To: roth@oasys.dt.navy.mil (Pete Roth)
  8. Organization: The David Taylor Model Basin
  9. Lines: 82
  10.  
  11. I must have left my brain home...
  12.  
  13. What I want to do is shown in the example code below. In words:
  14. Given the pointer to a derived object P,
  15. and Q declared as a pointer to an ancestor object type,
  16. HOW DO I INSTANTIATE Q AS A CLONE OF P ???????
  17.  
  18. Going crazy...
  19. -----Cut-here--------Cut-here--------Cut-here--------Cut-here-------Cut-here----
  20. uses Objects;
  21.  
  22. type
  23.  PBase = ^TBase;
  24.  TBase = object(TObject)
  25.    constructor Init;
  26.    constructor Clone( B : TBase ) ;
  27.    function    IsA : STRING ; VIRTUAL ;
  28.  end;
  29.  
  30.  PDerived = ^TDerived;
  31.  TDerived = object(TBase)
  32.    constructor Init;
  33.    constructor Clone( D : TDerived ) ;
  34.    function    IsA : STRING ; VIRTUAL ;
  35.  end;
  36.  
  37. constructor TBase.Init;
  38. begin
  39.  inherited Init;
  40. end;
  41.  
  42. constructor TBase.Clone( B : TBase ) ;
  43.    begin
  44.       Self := B ;
  45.    end ;
  46.  
  47. function TBase.IsA : STRING ;
  48.    begin
  49.       IsA := 'TBase'
  50.    end ;
  51.  
  52. constructor TDerived.Init;
  53. begin
  54.  if not inherited Init then
  55.    Fail;
  56. end;
  57.  
  58. constructor TDerived.Clone( D : TDerived ) ;
  59.    begin
  60.       Self := D ;
  61.    end ;
  62.  
  63. function TDerived.IsA : STRING ;
  64.    begin
  65.       IsA := 'TDerived'
  66.    end ;
  67.  
  68. var
  69.  P, Q, R: PBase ;
  70. begin
  71.    P := New(PDerived, Init);
  72.    if P <> nil then begin
  73.      Writeln( 'P is a ', P^.IsA ) ;
  74.      { how do I write a statement that instantiates Q as a PDerived
  75.        without specifying that TYPE, and without typecasting the Clone
  76.        method ? For example: }
  77.      Q := New( PDerived, Clone( PDerived(P)^ )) ;
  78.      Writeln( 'Q is a ', Q^.IsA ) ;
  79.      { works, but what I really want is something like
  80.         R := New( X, Clone( P^ )) ;
  81.        I've tried
  82.         R := New( TypeOf(P^) , Clone( P^ )) ;
  83.        etc. What am I forgetting ?
  84.      }
  85.    end ;
  86. end.
  87. -----Cut-here--------Cut-here--------Cut-here--------Cut-here-------Cut-here----
  88.  
  89. Grace & peace, pete
  90. - - - - - - - - - - - - - - - - - - - - - - - - - -
  91. Peter N Roth      roth@oasys.dt.navy.mil
  92. Do what you can with what you have where you are. - D L Moody.
  93.