473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Object counter for classes

ricfow
5
Suppose I have the following hierarchy of classes:

class Entity{
public:
Entity(int idValue){id=idValue};
private:
int id;
}

class Vertex: public Entity
{
public:
Vertex(): Entity(0){};
// More stuffs
}

class Edge: public Entity
{
public:
Edge(): Entity(1){};
// More stuffs
}

class Face: public Entity
{
public:
Face(): Entity(2){};
// More stuffs
}

The aim is to implement an object counter for classes Vertex, Edge and Face. This is very import in the context of my project. I figured out the following redesign:

class Entity{
public:
Entity(int idValue){id=idValue; objectCounter[id]++};
~Entity(){objectCounter[id]--};

int howManyObjects(){
return objectCounter[id];
}
private:
int id;
static int objectCounter[3];
}

int Entity::objectCounter[3] = {0,0,0};

Before my questions, a certain improvement in the code style would be to create enum {VERTEX,EDGE,FACE} and substitute the magic numbers 0, 1 and 2.

In the article I considered the proposed approach too much complicated by using templates. Supposing that the counters are only for objects of these classes (or a fixed number of classes that I known a priori), do you have any criticism about my approach, possible improvements and mostly important, potential pitfalls?
Your help will be much appreciated.
Mar 19 '10 #1
9 3061
jkmyoung
2,057 Expert 2GB
What would be the point of the enumeration, if you are still going to access the counter with an array?

Is space an issue?
Your declaration does leave Entity as a public class; other people will be able to call Entity(1), Entity(2), without being of the specified type.
Mar 19 '10 #2
ricfow
5
<What would be the point of the enumeration, if you are still going to access <counter with an array?
The enumeration is to avoid using the numbers 0,1 and 2. The constructor of Vertex would be, for instance,
Vertex::Vertex() : Entity(VERTEX){}
instead of
Vertex::Vertex() : Entity(0){}

----

<Is space an issue?
<Your declaration does leave Entity as a public class; other people will be able to <call Entity(1), Entity(2), without being of the specified type.

Size is no an issue, but I would not like to spend unnecessary bytes. You are right, objects of class Entity cannot be instanciated. What do you suggest?

Thanks in advance,
Mar 19 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Tell me again what this object count if for? With that static variable, your design is single-threaded. Single-threading does not work with modern multi-threaded software.


However, assuming you need it, I suggest you remove the object count from the Entity class. Instead, write a Factory class that has methods for
CreateVertex(), CreateEdge(), etc. and keep the counts as data members of the Factory class. That way you know how many objects of each type the Factory has created. Check out the Parameterized Factory aspect of the Factory design pattern.

Ideally, CreateVertex() would return a Handle to a Vertex (Handle<Vertex>) and for beginners it would return a Vertex*. There is an article on Handle classes in the C/C++ Insights.
Mar 20 '10 #4
ricfow
5
First of all, thank you for the interest and suggestions.

The reason I tryied to introduce the objects counter is the following:
Each instanciated object must have a unique ID among all objects
of a certain class. The IDs must belong to the range [1,totalNumberOfObjets].
I figured out this strategy because it will be very easy to read/write the
objects data to the disc. Vertex, Edge and Face are not the only entities,
there are many others composing a geometric/topologic data structure.
The main feature of the data structure is that the objects have a lot
of pointers. For instance, a edge have pointers to two vertices, a face
have pointers to edges, etc. When the objects are being saved to the disc,
I must substitute the pointers address by numbers and that's when the ID
of the entities come in handy.

The problem with the counters is that when an object is deleted, I would
like to use its ID again. Of course, I could always generate new IDs (bigger)
but I would not like to follow this way. Instead, I'm thinking to use a stack
to keep track of the IDs associated to the deletes objetcs, and reusing them again
when new objetcs are created. Maybe there is a simpler solution...

If you agree with my solution, I could
move the role machinery for the counters and IDs generation
to the Factory you have suggested.

I hope I've made myself clear. Thanks again.
Mar 20 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
So, I suggest you write a singleton class that is responsible for supplying those unique IDs. This class can implement a data base of previously supplied IDs or it could implement an algorithm much like Microsoft's Guidgen.exe.

Read the C/C++ Insight article on the Singleton Design Pattern for tips on how to implement this.

Then, have your constructor in your class access the singleton to get the ID for the object being constructed.

At this point a Factory does not appear to be required.
Mar 21 '10 #6
ricfow
5
Wouldn't be interesting to create this singleton class and assign it
the following responsabilities:
1)- Create the entities (createVertex(), createEdge(),...)
2)- Delete the entities
3) Manage the IDs
?
The reason to include the deletion of the entities is to help to manage
the IDs, because it is necessary to keep track of the deleted ones.

thanks again.
Mar 22 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
What you are talkin about is using a parameterized factory. You don't need a singleton for that. Which, incidentally, I think is a very good idea. The reason is the factory can be created on the heap, the objects created, and then the factory can be deleted thereby keeping your runtime footprint small.

Expand|Select|Wrap|Line Numbers
  1. Factory* f = new Factory;
  2. Vertex* v = f->CreateVertex();
  3. //etc...
  4. delete f;
I suggested the singleton as a source of IDs so that the entire program could be assured that each ID was unique. Here the Factory is the one accessing the singleton.

You don't want the factory to be a singleton since you don't want to have it in existence for the entire program.
Mar 22 '10 #8
ricfow
5
In fact the factory must exist for the entire execution, because my program is a geometric editor. So, creating and deleting vertices, edges, etc happen all the times. So I believe that keeps the factory as a singleton is worthwhile.

I also believe that delegating the responsability of managing the IDs to the factory is also interesting. The additional cost would be that all objects must deleted using deleteVertex(myvertex), deleteEdge(myedge), etc.. also implemented in the factory.
In this case, it would be more than a 'factory', but something like a
geometricEntitiesManager or simply EntitiesManager ...
Mar 22 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
Well, there you go. Once again in the OO world there are multiple solutions. Just pick one and run with it.
Mar 22 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Paul Rubin | last post by:
I'd like to have a function (or other callable object) that returns 0, 1, 2, etc. on repeated calls. That is: print f() # prints 0 print f() # prints 1 print f() # prints 2 # etc. ...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
4
by: lauch2 | last post by:
I have a project that contains a COM object running in the in-process configuration. The project was developed by VC6.0 with SP 3. The project has been put into production for long period of time...
6
by: Angelos | last post by:
Hello again, I have written three classes : validate.class.php, report_handler.class.php, sentry.class.php. The problem is that the "validate" class uses the "reporting" class and the...
7
by: Carsten H. Pedersen | last post by:
Hello I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object. Lets say...
0
by: freshman | last post by:
# test code: http://pyode.sourceforge.net/tutorials/tutorial3.html # #i want selecting object in pyode test code. # what's wrong? # under modify code # see selectObject() function # pyODE...
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
35
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to...
5
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.