home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16673 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.6 KB  |  80 lines

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: Help with nested class and constructor
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. Distribution: world
  7. X-Mailer: cppnews $Revision: 1.20 $
  8. Organization: Trumphurst Ltd.
  9. Lines: 66
  10. Date: Fri, 20 Nov 1992 17:33:30 +0000
  11. Message-ID: <722306010snx@trmphrst.demon.co.uk>
  12. Sender: usenet@gate.demon.co.uk
  13.  
  14. In article <Bxyxrv.EJ3@news.cso.uiuc.edu> macg9505@uxa.cso.uiuc.edu (Michael Alan Corn) writes:
  15. > I could use a little help on nested classes:
  16. >       I would like to include one of Borlands Array classes in another class,
  17. > in the private data section, but since the constructor requires an int to
  18. > initialize the class, I'm not sure how or where to give it the value:
  19. > class foo {
  20. >       private:
  21. >       Array myarray(10); ///this doesn't work, the compiler chokes
  22. You want ...
  23. class foo {
  24.       private:
  25.       Array myarray;
  26.  
  27. And in you constructor write ...
  28.  
  29. foo::foo(/* arguments, if any */)
  30. : myarray(10)    // Other initialisers
  31. {
  32. // Code, if any
  33. }
  34.  
  35. > so I've tried this, which seems to work
  36. > class foo {
  37. >       private:
  38. >       void *myarray;
  39. > and then in the constructor for foo I do the following:
  40. > foo::foo()
  41. > {
  42. > Array temp(10);
  43. > myarray = &temp;
  44. Oops - your void pointer points to a local variable on the stack. This 
  45. pointer will become invalid as soon as you return from the constructor.
  46.  
  47. > Fine, the compiler seems to like this, but now I can't figure out how to
  48. > access the member functions of the object I store in myarray.  I use the Array
  49. > function ptrAt to get a pointer to the object:
  50. > myarray->ptrAt(index) and then, since ptrAt returns a Object * I do the
  51. > following to get at that objects member function
  52. > myarray->ptrAt(index)->memfunc();  This doesn't work.  The compiler complains
  53. > about every "->"; I assume that this is a precedence problem, but I really
  54. > don't know.  Any help would be greatly appreciated.  Thanks
  55. > macg9505@uxa.cso.uiuc.edu
  56. But you have declared myarray as a void pointer. A void pointer is not an
  57. Array pointer. 
  58.  
  59. I think you would gain a lot from reading the C++ Frequently Asked 
  60. Questions list.
  61.  
  62. Many thanks to Marshall Cline for compiling and maintaining it. 
  63.  
  64. It can be obtained ...
  65.  
  66. Via ftp - Anonymous ftp from sun.soe.clarkson.edu [128.153.12.3] :pub/C++/FAQ
  67.  
  68. Via mail - send mail 
  69. To: archive-server@sun.soe.clarkson.edu
  70. Subject: send C++/FAQ
  71.  
  72. -- 
  73. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  74. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  75.