Variable within a class pointer

Asked

Viewed 603 times

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.

  • 1

    You should change your code so it can be created like this: Textbox* textBox1 = new Textbox();

  • 1

    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.

  • 1

    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.

  • 2

    And probably TD_ID_COUNTER should be a static function variable and not global. Besides it probably deserves another name and "Casing".

  • 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.

  • 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.

Show 1 more comment

1 answer

1

With the information you gave, I can only try to guess the reason. I can try to improve my answer if you clarify the two questions below:

  1. Your global variable declared this way: int TD_ID_COUNTER = 5000; is in a header or a file . cpp?
  2. You create two TextBox so, in sequence, as in this example of yours there, or you create them in separate files?

If the answers are: "1) in a header", and "2) in different files", I suspect you create several different instances of the variable TD_ID_COUNTER by including the header in different . cpp files. If this is the problem, the solution is to put in the header only:

extern int TD_ID_COUNTER;

and within some . cpp (and only one), the variable declaration:

int TD_ID_COUNTER = 5000;
  • The variable int 'TD_ID_COUNTER = 5000;' is in a cpp file of the library project, because it is only an internal counter I did not add the extern of the variable in the header file because it is used only within the library only generates new ids to differentiate each object. The variable that generates Ids works perfectly, each call of the function 'int Td_getid()' generates a new number, but the member 'int id;' of the class 'class Td_control' seems to point to the TD_ID_COUNTER so that when creating other objects, the previous objects have the same id, but id member is not pointer

  • Textboxes are created in sequence, but each time you create a textbox is called the 'Td_createcontroltextbox' function that creates the class with a new id generated by the return of the 'Td_getid' function()'.

  • 1

    Got it, so the problem shouldn’t be the variable TD_ID_COUNTER be instantiated repeatedly. By the way, since it is only used internally to the file, you should declare it as static int TD_ID_COUNTER = 5000;. Now I think the only way to find out the problem is to see the full body of your function TD_CreateControlTextBox(). Can you assure me that the 2 returned pointers are different? (std::cout << TextBox1 != TextBox2 << std::endl;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.