home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18604 < prev    next >
Encoding:
Text File  |  1992-12-31  |  1.9 KB  |  84 lines

  1. Path: sparky!uunet!munnari.oz.au!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Simulate "Pointer to char" by an array
  5. Message-ID: <803@ulogic.UUCP>
  6. Date: 31 Dec 92 18:58:58 GMT
  7. References: <1992Dec30.172241.11378@dcs.warwick.ac.uk>
  8. Organization: negligable
  9. Lines: 73
  10.  
  11. In article <1992Dec30.172241.11378@dcs.warwick.ac.uk> cmchan@dcs.warwick.ac.uk (C M Chan) writes:
  12. >
  13. >Hi out there,
  14. >I am sorry if this is faq for this newsgroup.   Here is my question:
  15. >How can I simulate "pointer to char" by an array in a class whose size will be \
  16. >delayed to be
  17. >defined at run time?
  18. >For example,
  19. >     class X {
  20. >     private:
  21. >             char string[      ];    /* not define size of the array */
  22. >                              ^^^^^^^^
  23. >     public:
  24. >             X(int);
  25. >             ...............
  26. >     };
  27. >     X::X(int size) {
  28. >             string[size];          /*  define size of string array now  */
  29. >             strcpy(string, "Hello");
  30. >     }
  31. >Any pointer to this problem will be greatly appreciated.   Thanks in advance.
  32. >
  33. >
  34. >
  35. >-- 
  36. >C M CHAN         cmchan@dcs.warwick.ac.uk
  37.  
  38.  
  39. Why? what is wrong with:
  40.  
  41.      class X {
  42.  
  43.      private:
  44.              char *string;
  45.  
  46.      public:
  47.              X(int);
  48.  
  49.              ...............
  50.      };
  51.  
  52.  
  53.      X::X(int size) {
  54.              string = new char[size];  /*  define size of string array now  */
  55.              strcpy(string, "Hello");
  56.      }
  57.  
  58.  
  59. Am I missing something about your requirements?
  60.  
  61.  
  62. btw: I apologize if this is posted twice - I did one w/ 
  63. a "malloc(size)" instead of new (oops! my C is showing!)
  64. but it didn't show up on the reader here, so I didn't
  65. cancel it....
  66.  
  67.  
  68. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  69. Philosopher and plowman            |
  70. each must know his part            |    -Richard Hartman    
  71. to build a new mentality        |    hartman@uLogic.COM
  72. closer to the heart!            |
  73.  
  74.