Originally Posted by
Codepoet
Here you check index == 3, but index is a static member. When there are exactly three instances any call to something will call incrNumber.
Yeap you're right I was thinking of something to count the instances of the class and this was the natural way, but unsusable... What about this one?
class myClass {
public:
myClass():perInstanceIndex(allClassesIndex){++allClassesIndex;}
myClass(const myClass&):perInstanceIndex(allClassesIndex){++allClassesIndex;}
~myClass(){--allClassesIndex;}
int number;
void incrNumber() {number++; }
void something();
private:
static size_t allClassesIndex;
size_t perInstanceIndex;
};
size_t myClass::allClassesIndex=0;
myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
if(perInstanceIndex==3)
incrNumber();
}
class myClass {
public:
myClass():perInstanceIndex(allClassesIndex){++allClassesIndex;}
myClass(const myClass&):perInstanceIndex(allClassesIndex){++allClassesIndex;}
~myClass(){--allClassesIndex;}
int number;
void incrNumber() {number++; }
void something();
private:
static size_t allClassesIndex;
size_t perInstanceIndex;
};
size_t myClass::allClassesIndex=0;
myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
if(perInstanceIndex==3)
incrNumber();
}
To copy to clipboard, switch view to plain text mode
Bookmarks