avoid destructor

G

Guest

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
 
V

Victor Bazarov

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
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top