qu******@yahoo.com wrote in news:ead1f717.0309281504.1bfcb399
@posting.google.com:
I am working on windows and create a dll. In side the dll, there is
something like
class B;
class A{
public:
B* GetB(){
B* pB = ...
return B;
}
};
Now the user use my dll:
A a;
B* pB = a.GetB();
...
delete pB;
There is a problem here. Since pB is allocated inside the dll. It
should be delete inside the dll too. But when the user call delete pB,
he is deleting it outsite the dll. How can I handle this?
This is a hack but Standard conforming hack, add operator new and
operator delete to your defenition of B that way all invocations of
new and delete will use whatever code the dll would use for new and
delete.
<example omit="platform-dependant-dll-cruft">
class B
{
//...
public:
void *operator new ( unsigned sz );
void operator delete ( void *p );
};
// in a TU compiled into your dll
void *B::operator new ( unsigned sz )
{
return ::operator new ( sz );
}
void B::operator delete ( void *p )
{
::operator delete ( p );
}
</example>
There are other platform dependant ways off doing what you want, they
may be more effecient, you should ask about those elsewhere eg:
news://comp.os.ms-windows.programmer.win32
HTH
Rob.
--
http://www.victim-prime.dsl.pipex.com/