class myClass {
myClass(){++index;}
myClass(const myClass&){++index;}
~myClass(){--index;}
int number;
void incrNumber() {number++; }
void something();
private:
static size_t index;
};
size_t myClass::index=0;
myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
if(index==3)
incrNumber();
}
class myClass {
myClass(){++index;}
myClass(const myClass&){++index;}
~myClass(){--index;}
int number;
void incrNumber() {number++; }
void something();
private:
static size_t index;
};
size_t myClass::index=0;
myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
if(index==3)
incrNumber();
}
To copy to clipboard, switch view to plain text mode
You'll see some obvious things left out such as uninitialized number var in the ctor but you get the idea. You'll have to be extra carerfull on how many objects are also created behind your back as they will be indexed too. I did not create a copy constructor to avoid indexing compiler generated objects.
[Edit]...witch is plain wrong as the destructor will decrease the index, I modified the source to also have a copy constructor.
Bookmarks