Topic: const int*const p cannot delete.
Author: zhn@aol.com (ZHN)
Date: 1999/05/23 Raw View
const int*const p cannot delete.
>You can't pass an array by reference because an array IS a reference. Remeber
>array names are pointers. Trying to pass an array using the & operator is
>lieteraly trying to pass a function a pointer to a pointer. And what's
>the...point...(ho ho) of that? Actually you can use pointers to pointers but
>you have to declare them specifically as such. Anyway...
>
>Dave
What seems here to be the problem is that you cannot get the address of a
pointer to an array. Thus you cant do this
int * p=new int[2];
(int*)& ref=p;//ILLEGAL
ref[0]=5;eg. //illegal
however this is a bigtime problem for c++
This means that there is no way to pass a pointer to an array of elements that
cannot be deleted!!!! Unless you pass it in a class. But still that does not
guarantee that it will not be deleted.
class foo
{
int *p;
};//end foo
int main()
{
foo boo;
foo &coo= boo;
thus coo.p=new int[10];
you could
delete coo.p;//Yes?
If so a problem for c++
This slows the language down and my suggestion becomes:
int *g= new int[10];
const int *const p = g;//now you have aconst
you CANNOT delete [] p;//this SHOULD BE ILLEGAL. BUT IT'S NOT. THIS IS A BIG
MISTAKE IF THE ANSI PEOPLE MADE IT.
return 0;
}
If you cant get a ref to a pointer then
const int *const p=new [10];//itself SHOULD BE ILLEGALl. You should not be able
to create an array using const so THAT YOU WOULD BE FORCED TO CREATE THE
ARRAY USING NON -CONSTANT THEREFORE BEING ABLE ALSO TO DESTROY IT WHEN
NECESSARY.
thus the scheme becomes
int *p= new int[X];
const int *const q=p;//perforce because you cannot create an array wiht a
const pointer under this scheme.
and then you have delete[] p; when finished. I mean you seem to have an array
that can go anywhere and do anything with NO FEAR OF DELETION. ISN'T THAT THE
BIGGEST STATED DRAWBACK OF C++. IS THIS NOT SAFER THAN CREATING REFS THAT MIGHT
LEAD TO THE ALL CONSUMING INVALID PROGRAM?
Without such rules I think C++ is slowed down considerably. I know that VC++
actually enforces the no delete const T* const p rule but it is actually not
compl;iant with ANSI.
If my understanding has failed here please let me know I am always seeking
to learn something new. Better to post it on the board though that way any
response helps more people.
ONCE AGAIN DOES ANYONE HAVE A WAY TO GET A PURE REF TO A POINTER TO AN ARRAY OR
EVEN AN ARRAY ALLOTED DYNAMICALLY?
Pete Thanks
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]