The difference is that first line is declaration of a pointer, visible only in current method, that is constructor. The recordNumberSpinBox member variable is not initialized, so you have a crash when trying to access it. If you want to use the spinbox in other methods, you should leave the declaration in the header, and initialize the member variable:
this
->recordNumberSpinBox
= new QSpinBox();
this->recordNumberSpinBox = new QSpinBox();
To copy to clipboard, switch view to plain text mode
Consider this example, it's just like your issue:
class A{
protected:
int var; //member variable, every object of class A has its own var
public:
A(){
this->var = 10; // initializes this object member variable with value 10
int var = 1; // creates local variable named var, visible only in this constructor
std::cout << "var = " << var << ", this->var = " << this->var << "\n";
// prints: var = 1, this->var = 10
}
void method(){
int x = var + 1; // x = 11, because this objects member variable is used
}
};
class A{
protected:
int var; //member variable, every object of class A has its own var
public:
A(){
this->var = 10; // initializes this object member variable with value 10
int var = 1; // creates local variable named var, visible only in this constructor
std::cout << "var = " << var << ", this->var = " << this->var << "\n";
// prints: var = 1, this->var = 10
}
void method(){
int x = var + 1; // x = 11, because this objects member variable is used
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks