Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 01:25 PM
David Belohrad
Guest
 
Posts: n/a
Default sharing resources over inherited classes

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
  #2  
Old November 10th, 2006, 04:55 PM
Davlet Panech
Guest
 
Posts: n/a
Default Re: sharing resources over inherited classes

David Belohrad wrote:
[...]
Quote:
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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles