Numbered class

J

jalina

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
 
I

Ian Collins

jalina said:
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.
 
Z

ziman137

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
 
F

Fei Liu

jalina said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top