pahill@glacier.sim.es.com (Paul Hill) (04/03/91)
This question is a lot like a the classic C FAQ of figuring out arrays and
pointers, but it involves the _new_ operator.
"The new operator returns a pointer to the object created." E&S p59
My problem is that I wish to create a table of ten pointers to things.
(Not the things just a dynamically allocated table of things).
Just to clear things up before anyone tries to muddy them:
"When an object of a nonclass type (including *arrays* of class
objects) is created with the operator new, the global ::operator new() is
used." E&S p60
Given the above information what the heck would one think that a
pointer to a table of pointers to Things would be? The following
works under Saber C++ and Glokenspiel C++ (DecStation Ultrix &
MipsOS).
Instead of an automatic or static table of pointers to things as in:
Thing * atable[10];
I want a pointer to a dynamic (heap) object that works the same:
atable[2]->memberElement; // an auto/static table
and
atable[2]->memberElement; // a heap table.
The following works:
Thing **table = &new (Thing *)[10];
Lets see that means that:
table is a pointer to a pointer to a Thing.
Gee, that seems right.
Similarly: (ignoring the &)
new will return a pointer (ref 1 above) to
an array of 10 pointers to Things.
QUESTION:
*** What the heck is the & (address of operator) doing there? ***
*** Why does it work? (Saber and Glokenspiel). ***
*** Why not with &?? ***
Is there something I'm missing.
To me with an & it would read as: the address of a pointer (returned by
new) to a table of pointers to Things.
Meanwhile, trying "objects of general type can be expressed using the
explicitly parenthesized version of the new operator." (E&S p62)
I'd think that the following would be right:
Thing ** table = new (Thing*[10]));
But it results in:
f856: "t.cc", line 31: Internal Error: bad base type: for
^^???????
Which is not even a complete error message.
Both:
Thing ** table = new ((Thing*)[10]);
Thing ** table = new ((*Thing)[10]);
Result in syntax errors.
Any thoughts would be appreciated.
thanks,
-Paul
pahill@glacier.sim.es.com
--
****
"In every age and clime we see / Two of a trade can never agree" - John Gay
Paul A. Hill Evans and Sutherland Computer Corp. (801)582-5847
UUCP: { ... | boulder }!utah-cs!esunix!pahill
Internet: esunix!pahill@cs.utah.edu or pahill%esunix@cs.utah.edu
Bitnet: esunix!pahill.UUCP@UTAHCCA or pahill%esunix.UUCP@UTAHCCA
****