I am unsure where to put this question, so I placed it here. If its in the wrong section, please let me know :)
Is it possible to have a static variable in a base class function. And, whenever any derived class calls that function, the static variable is the in all accesses? For example:
class BaseClass : CControl
{
declare CreateEx(int l, int t, int w, int h, int style, string title, CWindow *parent), int;
}
BaseClass :: CreateEx(int l, int t, int w, int h, int style, string title, CWindow *parent), int
{
static int Count = 1;
CControl!!Create(l, t, w, h, style, Count, title, Parent);
Count += 1;
return Count;
}
Class Class1 : BaseClass
{
// whee?
}
Class Class2 : BaseClass
{
// Whee, oh joy!
}
Now, whenever I want to create a control, the return value should always be 1+ Like
Class1.CreateEx(args); // Should return 1
Class2.CreateEx(args); // Should return 2 ... etc
Is this possible? I'm trying to emulate a way that I do not need to worry about ID numbers, while the return is a number. Or maybe even go as far as remove Ids (from the programmer view) entirely. Only dealing with pointers to objects instead :) Is this possible or could I b grasping at air? :) It's been a very long week. :-\
Looks like it will work. Although it is the first time I have seen anyone use a static variable like that ;)
Thanks, I was hoping I am on the right track. My next goal is to then, do a simple wrap on existing controls with a program wide static variable. This would have all controls (on creation) return a unique id. Hopefully, I'll get to the point of releasing just a single include or source file with this, if people are interested in such.
Just keep in mind that what you're doing there is not thread safe. You should use the Interlocked APIs to manipulate that variable (eg: InterlockedIncrement, InterlockedDecrement). While you could generally get away with that on a single-core/proc system due to the nature of the x86 architecture (something that I would argue you shouldn't depend on, either), it will definitely bite you on the arse with multi-core/proc systems.
I currently have a multi core system for development :) It's working. But then again, I dont use threads in my aurora ports yet :) Thanks for the tip when I do use threads though.