home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pascal / 6687 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  3.2 KB

  1. Path: sparky!uunet!news.tek.com!vice!bobbe
  2. From: bobbe@vice.ICO.TEK.COM (Robert Beauchaine)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: BP/TP OOP is missing something...
  5. Message-ID: <10781@vice.ICO.TEK.COM>
  6. Date: 18 Nov 92 17:43:18 GMT
  7. References: <dmurdoch.282.722020256@mast.queensu.ca> <10773@vice.ICO.TEK.COM> <dmurdoch.171.722054313@mast.queensu.ca>
  8. Organization: Tektronix, Inc., Beaverton,  OR.
  9. Lines: 69
  10.  
  11. In article <dmurdoch.171.722054313@mast.queensu.ca> dmurdoch@mast.queensu.ca (Duncan Murdoch) writes:
  12.  
  13. >On the other hand, it seems that it would be rather inefficient in lots of 
  14. >code.  For example, I often write routines that need to do calculations 
  15. >before they can call the constructor for an object; if it had already been 
  16. >constructed, would I have to destroy it before changing it?  A simple 
  17. >example would be reading a matrix from a file:  I might read a line to get 
  18. >the matrix dimensions, construct an appropriate matrix, and then read the 
  19. >data into it.  How are situations like that handled with automatic 
  20. >constructors?  Or is the automatic construction optional?  If so, that just 
  21. >leads to code equivalent to the TP code.
  22. >
  23.  
  24.   Constructors come in many flavors in C++.  There are default
  25.   constructors, copy constructors, and explicit constructors.
  26.  
  27.   Default constructors are called when you don't initialize an
  28.   object at its declaration:
  29.  
  30.      anobject x;  /* The default constructor is called here */
  31.  
  32.   For your matrix example, the default constructor would initialize
  33.   you dynamic memory pointers to NULL, set your dimenension(s) to 0,
  34.   and whatever else is required when you don't know the size of your
  35.   matrix.  
  36.  
  37.   Copy constructors are used to create an object from an already
  38.   existing ojbect:
  39.  
  40.     anobject x;
  41.     anobject y = x;  /* Copy constructor called here */
  42.  
  43.   This is preferable to a bitwise copy, especially for classes like
  44.   a matrix where you want to copy the information from y, but don't
  45.   want to create two pointer references to the same memory.
  46.  
  47.   And finally, there are explicit constructors:
  48.  
  49.     anobject x(3,column1,column2,column3)
  50.  
  51.   Here, you are calling a (previously defined) constructor that
  52.   accepts a dimension and a list of the column vectors out of which
  53.   to build the matrix.
  54.  
  55.   In all of these examples, if the objects were allocated on the
  56.   stack as in a function, they would automatically be destroyed when
  57.   you fell off the end of the function code; no fuss, no muss, no
  58.   objects accidentally left over gobbling up memory.
  59.  
  60.   Your matrix example is a poor one to use as an example of automatic
  61.   construction, since a generic matrix class doesn't have a size
  62.   until you exlicitly give it one (what's a default matrix?) .  
  63.   Many other objects don't have this problem, and automatic 
  64.   construction would alleviate the programmer of the task of creating 
  65.   and destroying them.
  66.  
  67.  
  68. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ 
  69.  
  70. Bob Beauchaine bobb@vice.ICO.TEK.COM 
  71.  
  72. "Look, I tried the cat experiment.  On the third trial, the cat was
  73. dead.  On each of the subsequent 413 trials, it remained dead.  Am I
  74. doing something wrong?"
  75.             James Nicoll
  76.  
  77. Q.  How many Heisenbergs does it take to screw in a light bulb?
  78. A.  If you know the number, you don't know where the light bulb is!
  79.  
  80.