| re: delete operator
* Radde:[color=blue]
> #include<iostream>
> using namespace std;
>
>
> class A{
> private:
> int* iArray;
> int size;
> public:
> A()
> {
> cout<<"No argument constructor"<<endl;
> }
>
> A(int s) : size(s)
> {
> cout<<"One argument constructor"<<endl;
> iArray = new int[size];
> }
>
> ~A()
> {
> cout<<"Destructor"<<endl;
> delete[] iArray;
> }
> };
>
>
> int main()
> {
> A a;
>
> delete a; // Error
> return 0;
> }[/color]
When posting code: please indent the code systematically.
[color=blue]
> In the above code, when i delete a( a object allocated on stack). What
> error compiler supposed to give.[/color]
The compiler is required to issue an error diagnostic, because you cannot
apply 'delete' to a non-pointer.
And 'a' is not a pointer.
The specific error message text (if any, we can e.g. hypothesize a compiler
employing beep codes, although that would probably not endear the compiler
to rock-music-loving hippie programmers) depends on the compiler.
[color=blue]
> First of all, is this allowed in C++.[/color]
No.
[color=blue]
> If not, does all compiler gives error, if so what error??[/color]
See above.
Btw., if you change your main program to
int main()
{
A a;
}
you will have a program that the compiler has to accept, but which
will exhibit the dreaded Undefined Behavior, UB, because the A default
constructor does not initialize the pointer that the destructor gives
to 'delete[]'.
The standard places no requirement on such a program, but a quality
compiler may ensure that you (probably) will get some run-time error.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |