sharing resources over inherited classes

Discussion in 'C++' started by David Belohrad, Nov 10, 2006.

  1. Dear All,
    could someone give a hint? I'd like to share the resources as follows:


    A shared class which works as counter of number of shared resources:

    class Shared
    {
    public:
    Q_PCBShared() : n_count (1) { };
    virtual void ref () {n_count++; };
    virtual bool deref () { --n_count; return !n_count; };
    virtual ~Q_PCBShared() {};
    int n_count;
    };

    then

    class A : public Shared
    {
    private:
    class Data : public Shared
    {
    public:
    // default constructor
    Data () { do something -> assignments}
    // storage variables
    int x,y,z,blabla;
    }

    protected:
    Data *varA, *varB;

    public:
    // constructor
    A( blabla ) : varA(new Data()), varB(new Data()) { some assignment with
    blabla}
    ~A ()
    {
    // deletes variables when no more referenced
    if (varA->deref())
    delete varA;
    if (varA->deref ())
    delete varB;
    }

    A& operator= (A& x)
    {
    if (varA->deref())
    delete varA;
    if (varA->deref ())
    delete varB;
    // now assign the stuff:
    varA = x->varA;
    varB = x->varB;
    varA->ref();
    varB->ref();
    }
    }

    so far this works quite ok for dynamic and non dynamic creation of class
    A. However, consider the case of using class A to construct another
    class, which is using these parameters (that's why I've inherited Shared
    also for A):

    class B
    {
    B( A& x, A& y)
    {
    classA = &x;
    classB = &x;
    // NOW I'd like to use reference to count A and B
    // objects in order to share them like seen in
    // operator:
    classA->ref();
    classB->ref();
    }

    B& operator= (B& x)
    {
    // we'd like to share the variables:
    if (classA->deref())
    delete classA;
    if (classB->deref())
    delete classB;

    classA = x.classA;
    classB = x.classB;
    classA->ref();
    classB->ref();
    }

    A *classA, *classB;
    }


    So my idea was to implement this mechanism over this hierarchy in order
    to save the space (there will be really many objects like those), but
    this algorithm certainly fails for class B because - let's have
    following function:

    B funcPanic ()
    {
    A x(blabla); // creates static local class A
    A y(blabla); // another of the same type
    // ^^^^^^ those should have each its own private varA and varB,
    // which are independently created. Reference counter is one
    // for each of them, and one for the classes itselfs

    B dummycrap = B(x,y);
    // ^^ sofar works because we take addresses of x and y and we
    // increase number of references of !!A class only!! (thus
    // while x and y will have 2 references, its variables will have
    // each just one reference!!!

    return dummycrap;
    }

    main()
    {
    B frufru = funcPanic();
    // ^^^^ this will fail because when exiting the funcPanic, the
    // two local variables are destroyed thus the frufru contains
    // invalid classA and classB pointers!
    }

    And this situation will not change even if I increase the number of
    references for varA and varB when calling reference of class B. This is
    because when exiting the function the pointers to x and y will no more
    exist (as we are not able to make conditional destructor and kill the
    objects only if they are not referenced elsewhere), while the varA and
    varB for each class A would be ok in this case.


    My question is: is there any heal to this problem? How'd you solve
    such a data sharing? I think that in order to make this functioning I
    can never use static local variables, but this is not really the goal
    because then it becomes (i think) extremelly messy to clean up after
    each creation of class.


    thanks for any ideas
    david
    David Belohrad, Nov 10, 2006
    #1
    1. Advertising

  2. David Belohrad wrote:
    [...]
    > My question is: is there any heal to this problem? How'd you solve
    > such a data sharing? I think that in order to make this functioning I
    > can never use static local variables, but this is not really the goal
    > because then it becomes (i think) extremelly messy to clean up after
    > each creation of class.
    >


    You obviously can't prevent local variables from being destroyed with
    reference counting, the only way to do it is to allocated your object
    with the "new" operator. As for the cleanup, there are ways to make it
    automatic: take a look at the Boost smart pointer library
    (http://www.boost.org).

    D.
    Davlet Panech, Nov 10, 2006
    #2
    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. Edwin Knoppert

    Sharing resources, practical? (webfarm)

    Edwin Knoppert, Feb 13, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    Edwin Knoppert
    Feb 14, 2006
  2. Scorpio

    Sharing resources

    Scorpio, Aug 7, 2003, in forum: Java
    Replies:
    0
    Views:
    328
    Scorpio
    Aug 7, 2003
  3. Martin Gregersen

    Sharing Resources (Globalization/Localization)

    Martin Gregersen, Nov 10, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    303
    Martin Gregersen
    Nov 10, 2006
  4. 7stud --
    Replies:
    11
    Views:
    385
    7stud --
    Nov 9, 2007
  5. Trevoke
    Replies:
    10
    Views:
    194
    Brian Candler
    Jul 22, 2009
Loading...

Share This Page