473,507 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 10 '06 #1
1 2005
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1626
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
9
2341
by: Reuben | last post by:
I have a field in an abstract class marked readonly. However, when I attempt to modify that field in the constructor of a class which inherits it, I get a compile-time error stating that 'A...
6
1747
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
3
5459
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
1
1503
by: Joe | last post by:
While I understand that Server Side Includes still work, I realize it's not the best practice for sharing code such as a common set of includes between screens. In ASP.NET I've already built...
10
1929
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
14
2609
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
0
3635
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
12
2063
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.