By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,335 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,335 IT Pros & Developers. It's quick & easy.

sharing resources over inherited classes

P: n/a
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
Nov 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Nov 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.