473,386 Members | 1,721 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,386 software developers and data experts.

Numbered class

Hello,

I want to have classes that all have a unique identifier. It is easy for
a single class:

class A {
static int index;
int my_index;
A(): my_index(index++) { }
};
But if you have something like:

class B: public A {
B() //....
}

class C: public A {
C() //....
}

the "index" is shared among A, B and C's. How can I do to have different
index for each distinct class without having to redefine each time a new
index ?

Thank you from Jalina
Apr 21 '06 #1
3 1333
jalina wrote:
Hello,

I want to have classes that all have a unique identifier. It is easy for
a single class:

class A {
static int index;
int my_index;
A(): my_index(index++) { }
};
But if you have something like:

class B: public A {
B() //....
}

class C: public A {
C() //....
}

the "index" is shared among A, B and C's. How can I do to have different
index for each distinct class without having to redefine each time a new
index ?

You can't.

You have to have one per derived class.

--
Ian Collins.
Apr 21 '06 #2
Maybe you can try this way:

1) build another Index class:

class Index {
static int index = 0;
int my_index;
Index(): my_index( index++ ) {};
int val() { return my_index; };
// ...
}

2) wrap Index objects in your derived classes:

class B: public A {
Index indObj;
int getIndex() {...}; // return indObj.val();
B() //....
}

class C: public A {
Index indObj;
int getIndex() {...}; // return indObj.val();
C() //....
}

3) for each object "B b()", or "C c()" ... associated index by

b.getIndex(), ...

Just a quick idea, didn't try it - supposedly to have lots of glitches,
;) you may work it out. My quick thought is about two guidelines:

1. never try and prevent manually constructing Index objects, so it is
better hidden and wrapped as private in another namespace, while
accessible by B, C, ....
2. depending on the context of your application, you may let index
monotonically gorw, or not.

Another idea is to use smart-pointer, reference pointer counting
pattern, which may need a bit more detailed work-out. Anyway, these're
just some quick brainstorming.

Gary
-: peace

jalina wrote:
Hello,

I want to have classes that all have a unique identifier. It is easy for
a single class:

class A {
static int index;
int my_index;
A(): my_index(index++) { }
};
But if you have something like:

class B: public A {
B() //....
}

class C: public A {
C() //....
}

the "index" is shared among A, B and C's. How can I do to have different
index for each distinct class without having to redefine each time a new
index ?

Thank you from Jalina


Apr 21 '06 #3

jalina wrote:
Hello,

I want to have classes that all have a unique identifier. It is easy for
a single class:

class A {
static int index;
int my_index;
A(): my_index(index++) { } Add a public interface here:
int getIndex() const { return index; } };
But if you have something like:

class B: public A {
B() //....
}

class C: public A {
C() //....
}

the "index" is shared among A, B and C's. How can I do to have different
index for each distinct class without having to redefine each time a new
index ?
All your class A, B, C objects are already guranteed to have different
index, all that needs to be done is a way to expose its value.
Thank you from Jalina


Apr 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
Hi, I have an unmanaged VC7 app calling a C# dotnet service. I'm using the VS2003 generated proxy class derived from CSoapSocketClientT. The client app works fine for most of my customers, but...
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
6
by: James A. Donald | last post by:
In firefox, the following standards based css gives me a numbered definition list: dl {counter-reset: item1; } dt:before { counter-increment: item1; content: counter(item1) ". "; }
9
by: joba | last post by:
Hi, I am building a program to calculate vacations for employees. Can anyone show me how to write a VBA code to differentiate even-numbered (Eg 2005) years from odd - numbered(Eg.2006) years. ...
8
by: Zamdrist | last post by:
With as class and object oriented .Net is, is the concept of general modules days numbered? When did you last add a general module to your project, and why wouldn't you have used a class instead?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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
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.