473,662 Members | 2,454 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 2020
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
1632
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 handful of inherited classes. The static method calls the ctor of the appropriate inherited class and returns it as type IBaseClass. There are no new methods or properties in the inherited classes and public clients never need to know which...
9
2348
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 readonly field cannot be assigned to (except in a constructor or a variable initializer).' Any ideas would be apreciated, though I was hoping I wouldn't have to use a property. Reuben.
6
1761
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 the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
3
5470
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. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
1
1506
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 several User Controls and such for sharing code between screens, but this typically was used things such as menus and headers and footers. What is the best practice in 1.1 for sharing code such as a common set of functions used by multiple screens?
10
1951
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, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
14
2626
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 Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
0
3659
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 delegates were given CDs containing a pre-release version of the .NET framework/SDK and Visual Studio.NET. · When was the first version of .NET released? The final version of the 1.0 SDK and runtime was made publicly available around 6pm PST on...
12
2083
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 with the following: Can this create any problems with GNU g++ compilation, linking etc? What would be the impact to the performance of the system? Anything else we have to consider?
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2762
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 we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.