Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > avoid destructor

Reply
Thread Tools

avoid destructor

 
 
Guest
Posts: n/a
 
      07-27-2004
I have this class:
---------------------------------------------
class A {
string a;
int instances;
A();
static vector<A> all;

public:
static A *CreateA(...................);

void operator delete(void *tex) {
A *t = (A*) tex;
if (t->instances) t->instances--;
}

static void GarbageCollector();
};
----------------------------------------------

CreateA checks if "string a" exists in any of vector's objects. If exists
increases counter and return Pointer else creates and adds in vector a new
object.
When I run "delete(objectA);" then C++ runs first destructor and after
"delete".
This means that "string a" destroyed.
How can I avoid this? Because in this pointer there are many objects and
"string a" destroyed for all. (avoid to run destructor, but run only
"delete")

Thanks


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-27-2004
<- Chameleon -> wrote:
> I have this class:
> ---------------------------------------------
> class A {
> string a;
> int instances;
> A();
> static vector<A> all;
>
> public:
> static A *CreateA(...................);
>
> void operator delete(void *tex) {
> A *t = (A*) tex;
> if (t->instances) t->instances--;
> }
>
> static void GarbageCollector();
> };
> ----------------------------------------------
>
> CreateA checks if "string a" exists in any of vector's objects. If exists
> increases counter and return Pointer else creates and adds in vector a new
> object.
> When I run "delete(objectA);" then C++ runs first destructor and after
> "delete".
> This means that "string a" destroyed.
> How can I avoid this? Because in this pointer there are many objects and
> "string a" destroyed for all. (avoid to run destructor, but run only
> "delete")


Don't call it "delete", don't make it an operator, but instead
make it a function and call it "destroy". Define the operator
as private so nobody can invoke it without your knowledge.

Victor
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      07-28-2004
On Tue, 27 Jul 2004 22:34:16 +0300, "<- Chameleon ->"
<(E-Mail Removed)> wrote:

>I have this class:
>---------------------------------------------
>class A {
> string a;
> int instances;
> A();
> static vector<A> all;
>
>public:
> static A *CreateA(...................);
>
> void operator delete(void *tex) {
> A *t = (A*) tex;
> if (t->instances) t->instances--;


By the time you get to operator delete the lifetime of the object has
ended, and accessing the instances member has undefined behaviour.

> }
>
> static void GarbageCollector();
>};
>----------------------------------------------
>
>CreateA checks if "string a" exists in any of vector's objects. If exists
>increases counter and return Pointer else creates and adds in vector a new
>object.
>When I run "delete(objectA);" then C++ runs first destructor and after
>"delete".
>This means that "string a" destroyed.
>How can I avoid this? Because in this pointer there are many objects and
>"string a" destroyed for all. (avoid to run destructor, but run only
>"delete")


Don't call delete on pointers returned by CreateA, instead add a
DestroyA static member to A and have an unimplemented private operator
delete to ensure that no-one does call delete by mistake.

A better strategy would be to use a smart pointer. e.g.

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using boost::shared_ptr;
using boost::weak_ptr;

class A
{
string a;
static std::vector<weak_ptr<A> > all;
public:
static shared_ptr<A> CreateA(..);
//...
};

See www.boost.org

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments