home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / pascal / 8469 < prev    next >
Encoding:
Text File  |  1993-01-23  |  5.1 KB  |  153 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!cs.utexas.edu!uwm.edu!linac!att!news.cs.indiana.edu!noose.ecn.purdue.edu!mentor.cc.purdue.edu!biomac1q.bio.purdue.edu!user
  3. From: pf@bilbo.bio.purdue.edu (Paul Furbacher)
  4. Subject: Re: Questions about Constructors and pointers to objects in TP 6
  5. Message-ID: <pf-230193014641@biomac1q.bio.purdue.edu>
  6. Followup-To: comp.lang.pascal
  7. Sender: news@mentor.cc.purdue.edu (USENET News)
  8. Organization: Purdue University
  9. References: <1993Jan22.171839.4925@cs.wm.edu>
  10. Date: Sat, 23 Jan 1993 07:34:59 GMT
  11. Lines: 140
  12.  
  13. Where do we start and how can we be gentle with the following?
  14.  
  15. In article <1993Jan22.171839.4925@cs.wm.edu>, 
  16.   greg@ms.cs.wm.edu (Gregory S. Miller) wrote:
  17. > Is it legal for the constructor for an object to call the 
  18. > constructor for a parent object?
  19.  
  20. As they say on TV (the tube, not the framework), "Read the book."
  21. Essentially, it says, yes.  If you have any of the source code 
  22. which comes with TP6.0 or BP7.0, read that also.  You'll see 
  23. many examples of this.  Precisely, this is what inheritance is
  24. all about, choosing to talk to your parents or not.  If you choose
  25. to converse with the ancestors, the question becomes *when*.
  26.  
  27. > I am having some difficulty with virtual method calls going to the wrong
  28. > class method, and was wondering if I might be accidentally overwriting
  29. > the pointer to the VMT by calling the parent class constructor from within 
  30. > the descendant class constructor.
  31. > A pointer of type pClass is assignment-compatible with ALL descendants of
  32. > Class, correct?
  33.  
  34. Correct, with the appropriate typecasting.  But you can get
  35. into trouble with this.  By typecasting, you're trying
  36. to fake out the type-checking mechanisms built into the compiler,
  37. and type-checking is one of the reasons you program in Pascal 
  38. and not something like C or BASIC.
  39.  
  40. > As a result, the following should work:
  41. >     New(pDescendantPtr,Init(Stuff));
  42.        ^          ^^^
  43.                   redundant: the "P" at the beginning means "ptr"!
  44.  
  45.    You've got to sit down with your manual, read it and, I'm sorry to
  46. say, comprehend it.  After that try the following:
  47.  
  48.    MyDescendent := New(PDescendent, Init(...)); {ellipsis, not "Stuff"}
  49.  
  50. >     pClassPtr := pDescendantPtr;
  51.  
  52. No, no, no.  Assuming you are following naming conventions (it's
  53. apparent that you are not) you are trying to assign types and 
  54. not variables.  Again, look at the redundancy in your naming. 
  55. (To re-iterate and paraphrase what S.A. Modena said yesterday in 
  56. another thread, "pay attention to naming; it will save you 
  57. tons of grief".)
  58.  
  59. >     pClassPtr^.VirtualMethod;  { executes Descendant.VirtualMethod, right? }
  60. > Would this still work?
  61. >     var
  62. >         Ptr    : Pointer;
  63. >         Classptr: pClass;
  64. >         Descend    : pDescendant;
  65. >     Classptr := Descend;
  66. >     Ptr := Classptr;
  67. >     pClass(Ptr)^.Virtualmethod;  { still execute Descendant.VirtualMethod? }
  68. > In my code, which I will not post yet, ...
  69.  
  70. Well, much of the trouble for us in these snippets is
  71. that we don't know if you've done many of the things needed
  72. to make any of this code work.
  73.  
  74. 1.  Unless you have made a correct call to "New(...)" you haven't a
  75.     chance of this working.  Remember, you are working with pointers.
  76.     This is very different from declaring "A : TClass;".  The latter
  77.     forces the compiler to allocate the static memory for "A".
  78.     When you use pointers, the compiler only puts aside sizeof(pointer)
  79.     for the dynamic variable.  You are responsible for allocating
  80.     the proper amount of memory for that dynamic (pointer) variable.
  81.     Therefore, you must call "New(...)".  Read about the extension
  82.     to the "New()" function in relation to objects.
  83.  
  84. 2.  Once you follow an example from the source code provided 
  85.     with TP/BP, you'll be able to test some of these ideas.
  86.     You already have the notion that you can typecast -- that's good.
  87.  
  88.     You can try the following:
  89.  
  90.     type
  91.       PBase = ^TBase; 
  92.       TBase = object
  93.         constructor Init(...);
  94.         destructor  Done; virtual;
  95.         function    IsA: string; virtual;
  96.       end; 
  97.  
  98.       PDerived = ^TDerived;
  99.       TDerived = object
  100.         constructor Init(...);      { if necessary }
  101.         destructor  Done; virtual;  { if necessary }
  102.         function    IsA: string; virtual;
  103.       end; 
  104.       ...
  105.       { provide method *definitions* -- learn }
  106.       { the lingo -- such as ...              }
  107.  
  108.       function TBase.IsA: string;
  109.       begin
  110.         IsA := " is an instance of TBase.";
  111.       end;
  112.  
  113.       function TDerived.IsA: string;
  114.       begin
  115.         IsA := " is an instance of TDerived.";
  116.       end;
  117.       ...
  118.  
  119.     var
  120.       aBase    : PBase;
  121.       aDerived : PDerived; 
  122.      
  123.     begin
  124.       aBase := New(PBase, Init(...));
  125.       Writeln("aBase", aBase^.IsA);
  126.  
  127.       aBase := New(PDerived, Init(...));
  128.       Writeln("aBase", aBase^.IsA);
  129.  
  130.       { try extending this by trying different }
  131.       { typecasts and with the variable        }
  132.       { "aDerived" -- see what happens.        }
  133.  
  134.       { properly dispose of the instance(s)    }
  135.       Dispose(aBase, Done);
  136.  
  137.     end.
  138.  
  139.  
  140. Others may have more to say on this, and some may castigate 
  141. me for being too harsh.  C'est la vie.
  142.  
  143. Paul Furbacher
  144. pf@bilbo.bio.purdue.edu
  145.