sharing resources over inherited classes

D

David Belohrad

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
 
D

Davlet Panech

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.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top