avoid destructor

Discussion in 'C++' started by Guest, Jul 27, 2004.

  1. Guest

    Guest 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
     
    Guest, Jul 27, 2004
    #1
    1. Advertising

  2. <- 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
     
    Victor Bazarov, Jul 27, 2004
    #2
    1. Advertising

  3. Guest

    tom_usenet Guest

    On Tue, 27 Jul 2004 22:34:16 +0300, "<- 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--;


    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
     
    tom_usenet, Jul 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Malkis
    Replies:
    8
    Views:
    537
    Alexander Malkis
    Apr 14, 2004
  2. frs
    Replies:
    20
    Views:
    785
    Alf P. Steinbach
    Sep 21, 2005
  3. arun
    Replies:
    2
    Views:
    567
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    437
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,181
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page