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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!usc!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!newsserver.jvnc.net!newsserver.technet.sg!nuscc!papaya!suresh
  3. From: suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar)
  4. Subject: Re: Need access to array index in ctor
  5. Message-ID: <1993Jan23.050800.20254@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Organization: Institute Of Systems Science, NUS
  8. References: <C1A1nx.K2L@undergrad.math.waterloo.edu>
  9. Date: Sat, 23 Jan 1993 05:08:00 GMT
  10. Lines: 47
  11.  
  12. In article <C1A1nx.K2L@undergrad.math.waterloo.edu> wgsteven@mobius10.math.uwaterloo.ca (Warren Stevens) writes:
  13. >I am creating a class, called block, that needs to know it's index in a
  14. >constructor if it is created as part of an array.  For instance,
  15. >
  16. >    House    block[512];
  17. >
  18. >would call block::block() 512 times.  Each time the constructor is called, 
  19. >i need to know what the index being created is.
  20. >
  21. >Is there some way to do this easily?
  22. >
  23. >Thanks,
  24. >
  25. >Warren
  26.  
  27. Off the top of my head ( no flames please :->) here's a simple hack ...
  28.  
  29. Define a static member variable in the class, set it to zero and increment
  30. it in the constructor .. a kind of class instance counter (this group has 
  31. seen  a major discussion on class instance counting so I'm not going to 
  32. restart the same thread )
  33.  
  34. Within the constructor, the value of the static counter variable index will 
  35. give you the index of the instance being instantiated. 
  36.  
  37. Assumptions:
  38. 1> No other instances (besides the array)have already been created.
  39. 2> The constructors for an array of classes declaration are called in order
  40.   of increasing index( Can this be guranteed ? Anyone ?). A simple way to
  41.   verify if this is happening will be to print out the value of
  42.   (this - counter). It should be the same each time .. i.e the address of the
  43.   zero'th variable of the array.
  44.  
  45.  
  46.       __                  
  47.      (_   / /  o_   o  o |_
  48.      __)/(_( __) (_(_ /_)| )_
  49.  
  50.  
  51. ***************************************************************************
  52. * Suresh Thennarangam               *  EMail: suresh@iss.nus.sg(Internet) *
  53. * Research Scholar                  *         ISSST@NUSVM.BITNET          *
  54. * Institute Of Systems Science      *  Tel:  (065) 772 2588.              *
  55. * National University Of Singapore  *  Facs.: (065) 778 2571              *
  56. * Heng Mui Keng Terrace             *  Telex: ISSNUS RS 39988             *
  57. * Singapore 0511.                   *                                     *
  58. ***************************************************************************
  59.