PDA

View Full Version : Objects and members



mickey
28th January 2006, 02:28
Hi,
I have a class:


class myClass {
int number;
void incrNumber() {number++; }
void something();
};
myclass c[3];

myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
}
Is there a way to modify the state of an instance of object only (and only one) from a method of myClass?
Thanks.

yop
28th January 2006, 03:15
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();
}

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.

mickey
28th January 2006, 10:58
thanks...Is there only this way? Is what I want to do a thing to avoid?
Thanks.

Codepoet
28th January 2006, 12:47
yop, your code will not work:

myClass::something() {
//here i'd like make something such as myClass[3].incrNumber();
if(index==3)
incrNumber();
}
Here you check index == 3, but index is a static member. When there are exactly three instances any call to something will call incrNumber.

mickey, you need here another mechanism: One static member for the current count of objects and some kind of management which instances exist (use as an ID their adresses or another unique ID) plus a class local member with the ID if you do not use the address. The real problem comes now - which is instance number 3? Imagine you created 5 instances and then delete (maybe they only left scope) instances 0 and 3. What now? In my appoach you could ask the static instance management but how should it decide which instance is number 3?
This gets even more complicated if you have threads.


Can you tell us what you need or want to do in more detail? It seems to me, that this approach is somewhat difficult.

wysota
28th January 2006, 13:29
Maybe something like this will do:


class xyz {
public:
static xyz *create(){ return new xyz(); };
~xyz(){ int ind = xyz::_instances.indexOf(this); xyz::_instances[ind]=0; }
void doSomething(uint ind){
if(xyz::_instances.size()<=ind || xyz::_instances.value(ind)==0)
return;
xyz::_instances[ind]->inc();
}
private:
xyz(){ xyz::_instances << this; ref = 0; };
static QList<xyz*> _instances;
void inc(){ ref++; }
int ref;
};

This makes sure you can't create an object on stack and stores all instances of the class in the class itself.

Edit: In the destructor the pointer in the list should be zeroed and a check for null pointer should be made before dereferencing it. I'll adjust the code...

yop
30th January 2006, 08:49
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 :o 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){++allC lassesIndex;}
myClass(const myClass&):perInstanceIndex(allClassesIndex){++allClassesIn dex;}
~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();
}

Codepoet
30th January 2006, 23:40
@yoy: Somewhat better but the destructor will "kill" you ;) You could end up with several instances having the same ID. I like wysota's solution but that will only work for "small" numbers of long living objects.

@mickey: can you write us more details about what you want to do or did we already solve your problem?

yop
31st January 2006, 09:19
@yoy: Somewhat better but the destructor will "kill" you ;) You could end up with several instances having the same ID. I like wysota's solution but that will only work for "small" numbers of long living objects.True again, I 'll give up on this :)

mickey
1st February 2006, 01:13
hi, I'm sorry for not fast reply.
I have 3 instances (a,b,c) of MyWidget class. I'd like a function of widget 'a' that change the state of 'b' and 'c'
I can use a variable index++ and an if (index==1) then....but after recognize the widget 'a', I must call a particular function member of widget 'b' and 'c'.
(but using index++: Are the order of the constructors the same every time???).