home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c++
- Subject: Re: Simulate "Pointer to char" by an array
- Message-ID: <803@ulogic.UUCP>
- Date: 31 Dec 92 18:58:58 GMT
- References: <1992Dec30.172241.11378@dcs.warwick.ac.uk>
- Organization: negligable
- Lines: 73
-
- In article <1992Dec30.172241.11378@dcs.warwick.ac.uk> cmchan@dcs.warwick.ac.uk (C M Chan) writes:
- >
- >Hi out there,
- >
- >I am sorry if this is faq for this newsgroup. Here is my question:
- >
- >How can I simulate "pointer to char" by an array in a class whose size will be \
- >delayed to be
- >defined at run time?
- >
- >For example,
- >
- >
- > class X {
- >
- > private:
- > char string[ ]; /* not define size of the array */
- > ^^^^^^^^
- > public:
- > X(int);
- >
- > ...............
- > };
- >
- >
- > X::X(int size) {
- > string[size]; /* define size of string array now */
- > strcpy(string, "Hello");
- > }
- >
- >Any pointer to this problem will be greatly appreciated. Thanks in advance.
- >
- >
- >
- >--
- >C M CHAN cmchan@dcs.warwick.ac.uk
-
-
- Why? what is wrong with:
-
- class X {
-
- private:
- char *string;
-
- public:
- X(int);
-
- ...............
- };
-
-
- X::X(int size) {
- string = new char[size]; /* define size of string array now */
- strcpy(string, "Hello");
- }
-
-
- Am I missing something about your requirements?
-
-
- btw: I apologize if this is posted twice - I did one w/
- a "malloc(size)" instead of new (oops! my C is showing!)
- but it didn't show up on the reader here, so I didn't
- cancel it....
-
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Philosopher and plowman |
- each must know his part | -Richard Hartman
- to build a new mentality | hartman@uLogic.COM
- closer to the heart! |
-
-