11
I have several header files with GUI management functions that I’ve done to create windows, similar to those libraries like GTK, QT and others, and I’m turning them into library, but I’m having a problem that’s going to be a little tricky to explain.
I have a class called Td_control that represents a controller as a button, textBox
and others, within it has a member called id which is an integer representing the id of the object in the loop
of messages, basically so far my class is like this:
class TD_Control
{
public:
int id;
... // Esta classe é muito grande, por isso abreviei
}
And I have a function called Td_getid() that generates a new id different from all generated and basically it sums the value of a variable returns a new value, like this:
// Esta variavel é global
int TD_ID_COUNTER = 5000;
int TD_GetID()
{
TD_ID_COUNTER++;
return TD_ID_COUNTER - 1;
}
And to create a control there is a function to create each type of control like this that creates a TextBox
:
TD_Control* TD_CreateControlTextBox()
{
TD_Control* ec = new TD_Control();
int nid = TD_GetID();
...// Esta classe é muito grande, por isso abreviei
return ec;
}
All these functions are within a Static library, I compiled and everything worked out, I created windows, controls and others, but when creating several controls and using events I had a problem, see this example:
// Tb1
TD_Control* TextBox1;
TextBox1 = TD_CreateControlTextBox();
// Tb2
TD_Control* TextBox2;
TextBox2 = TD_CreateControlTextBox();
When creating these controls, each one should have a different id, but all have the same id being all equal to 5002. Is it because the class object is a pointer and when using pointer object it has some different feature? Or is there something wrong with my code?
Unfortunately it is not possible to put the whole code because it is too big, only the main source code has 2156 lines.
You should change your code so it can be created like this: Textbox* textBox1 = new Textbox();
– Tony
Are you compiling everything into a single library or are there several partial libraries? This makes a difference when dealing with static variables, as there may be more than one "copy", one in each library.
– Guilherme Bernal
Your code for
TD_GetID()
is equivalent to:int TD_GetID() { return TD_ID_COUNTER++; }
. You’re using one more line, and one more subtraction.– lvella
And probably TD_ID_COUNTER should be a static function variable and not global. Besides it probably deserves another name and "Casing".
– Bacco
In the "Td_control* Td_createcontroltextbox()" function you are creating the new id like "int Nid = Td_getid();", but at least in this code snippet you nay is initiating the "id" member of the Td_createcontroltextbox class. Note that its Factory function nay is an instance of the class it is creating, so the variable "id" within Factory is a local variable of Factory, not a member of the instance being created.
– zentrunix
Tip: C++ has namespaces, so you can create a "TD" namespace, put everything in and get the same advantages you’re getting with the "TD" prefix, but without polluting your library’s client codes.
– Pablo Almeida