home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!pilchuck!fnx!sgihbtn!dgeary
- From: dgeary@sierra.com (David Geary)
- Subject: Re: dynamic array of pointers to a class type
- Message-ID: <1992Aug17.143621.29059@sierra.com>
- Sender: news@sierra.com
- Nntp-Posting-Host: grumpy
- Organization: Sierra Geophysics, Kirkland WA
- References: <ken.4@tlc-486.clemson.edu>
- Date: Mon, 17 Aug 1992 14:36:21 GMT
- Lines: 91
-
- In article <ken.4@tlc-486.clemson.edu> ken@tlc-486.clemson.edu (Ken Bauman) writes:
- >
- >(Using Borland C++ 3.1)
- >
- >Got a small problem here. I am trying in one class to create a list of
- >pointers to another class. The size of the list may vary widely upon
- >creation of the class containing the list. This seems obviously a job for
- >the constructor. However I can't quite seem to get it right. In the
- >following simple example the intent is for class B to create a list of
- >pointers to class A.
- >
- >class A { int i; };
- >
- >class B
- >{
- >private:
- > A *list[]; // list should be an open array of pointers to objects
- > // of type class A
- >public:
- > B(int);
- >};
- >
- >B::B (int size)
- >{
- > list = new A *[size]; // this (I think) should allocate the space for
- > // an array of pointers to class A
- > // However, I get a compiler error Lvalue required
- >}
- >
- >I can get around this by declaring in class B
- > A **list;
- >instead of
- > A *list[];
- >but this seems rather ungracefull and unintuitive? and perhaps bad
- >programming practice. I know that list is a pointer to an array of pointers
- >and thus may be considered a pointer to a pointer but it doesn't seem to me
- >to be quite the same thing.
- >
- >Can anyone provide some insight, both as to correctness and good programming
- >practice? Much thanks.
- >
- >Ken
- >
- > /---------------------------------------------------------\
- >/ Ken Bauman @ | My opinions are irrelevant. \
- >\ tlc-486.clemson.edu | Everything I say is fact. NOT! /
- > \---------------------------------------------------------/
-
- Ken,
-
- You are trying to change the address of an array, (remember, list
- is an *array* of pointers - [] has precedence over *) which is illegal.
- Imagine the havoc that would ensue, were one able to write the
- following code:
-
- main()
- {
- char someArray[100];
-
- strcpy(someArray, "hello");
-
- someArray = 5; // Illegal! [1]
-
- cout << someArray; // What gets printed here?
- }
-
-
- [1] - Since the name of an array in a value context is the same as
- the address of the first element in the array, what you are
- saying is:
-
- &someArray[0] = 5;
-
- What would be the effect of this sly maneuver? We're indicating
- that we want that 'h' placed in memory location 5. Do the
- rest of the 100 chars get "moved" also? What happens to whatever
- was at memory address 5? Due to such problems, the assignment
- is disallowed.
-
- Therefore, what you *must* declare (it's not a matter of good/bad
- programming practice) is a *pointer*, which is an Lvalue, which means
- you can write to it (the L stands for Loadable - we can load the
- expression with a value - some prefer to think of the L as "left", as
- in it's on the left hand side of an assignment). Arrays are
- Rvalues, which means they can only be *Read* from - some prefer
- to think of the R as "right", as in it's on the right hand
- side of an assignment.
- --
- David Geary | Seattle - America's most attractive city ...
- Sierra Geophysics |
- dgeary@sierra.com | to the jetstream ;-(
-