Re: auto_ptr and operator void*();
Gianni Mariani wrote in news:bkl6u2$erh @dispatch.conce ntric.net:
[color=blue]
>
> I think you're argument is lacking.
>
> r is an int * - the size is sizeof(int). The static_cast<int *>(q) put
> that information you're referring to back into the pointer. Hence the
> size of what r is pointing to is known at the time it is deleted.
>
> What do you think this should do ?
>
> int* p = new char(42);
> void* q = static_cast<voi d*>(p);
> MuthaObject * r = static_cast<int *>(q);
> delete r;
>[color=green]
>>
>> delete q;
>>[/color]
>
> the size of what q is pointing to is not known.
>
>[/color]
operator delete doesn't take size info, demo below.
The type info in delete is (can) only be used to call the destructor.
Also there would be little utility in banning delete (void *), as
all it really does is call the global operator delete( void * ). It
never needs the :: when used in a member function though.
#include <iostream>
#include <ostream>
struct base
{
virtual ~base() {}
void *operator new (std::size_t sz )
{
std::cout << "new; " << sz << std::endl;
return ::operator new( sz );
}
void operator delete ( void *p )
{
std::cout << "deleteing; " << p << std::endl;
::operator delete ( p );
}
};
struct derived: base
{
int i;
};
void del_base( base *b )
{
delete b;
}
int main()
{
base *b = new base;
del_base( new derived );
del_base( b );
}
Rob.
--
Gianni Mariani wrote in news:bkl6u2$erh @dispatch.conce ntric.net:
[color=blue]
>
> I think you're argument is lacking.
>
> r is an int * - the size is sizeof(int). The static_cast<int *>(q) put
> that information you're referring to back into the pointer. Hence the
> size of what r is pointing to is known at the time it is deleted.
>
> What do you think this should do ?
>
> int* p = new char(42);
> void* q = static_cast<voi d*>(p);
> MuthaObject * r = static_cast<int *>(q);
> delete r;
>[color=green]
>>
>> delete q;
>>[/color]
>
> the size of what q is pointing to is not known.
>
>[/color]
operator delete doesn't take size info, demo below.
The type info in delete is (can) only be used to call the destructor.
Also there would be little utility in banning delete (void *), as
all it really does is call the global operator delete( void * ). It
never needs the :: when used in a member function though.
#include <iostream>
#include <ostream>
struct base
{
virtual ~base() {}
void *operator new (std::size_t sz )
{
std::cout << "new; " << sz << std::endl;
return ::operator new( sz );
}
void operator delete ( void *p )
{
std::cout << "deleteing; " << p << std::endl;
::operator delete ( p );
}
};
struct derived: base
{
int i;
};
void del_base( base *b )
{
delete b;
}
int main()
{
base *b = new base;
del_base( new derived );
del_base( b );
}
Rob.
--
Comment