PDA

View Full Version : variable scope question



jeffmetal
16th June 2011, 19:46
I have declared a QSpinBox in the constructor of a dialog like this

QSpinBox *recordNumberSpinBox = new QSpinBox;

and also declared it as public: in the header file.

But im getting core dumps when i try to do anythting with this QSpinBox in any other methods.

for instance setting a maximum

recordNumberSpinBox->setMaximum(5); in the method

void Dialog::openDataFile(QFileInfo &fileInfo){
....
}

Am i confused in thinking that setting this to public means any method can access it and that its been set globaly?

squidge
16th June 2011, 19:58
So in effect you have two copies, one in your constructor and one in the class.

Perhaps what you should do is delete the one in your constructor so the class scope takes over, then you can use it in other methods.

Santosh Reddy
16th June 2011, 21:09
If you are still confused, after squidge's post, then the simple way say is modify your constructor like this


//QSpinBox *recordNumberSpinBox = new QSpinBox;
recordNumberSpinBox = new QSpinBox;

jeffmetal
16th June 2011, 22:11
If you are still confused, after squidge's post, then the simple way say is modify your constructor like this


//QSpinBox *recordNumberSpinBox = new QSpinBox;
recordNumberSpinBox = new QSpinBox;

Whats the diffence between the two lines above and should i also leave the QSpinBox *recordNumberSpinBox; in the header ?

stampede
16th June 2011, 22:27
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();

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
}
};

Santosh Reddy
16th June 2011, 22:29
Leave it in the header file

ChrisW67
16th June 2011, 22:41
The first line declares and initialises a variable in the local scope to point at a block of memory. This variable masks any other variable of the same name in a broader scope, e.g. a class member variable or global. The line variable becomes unavailable at the end of the scope enclosing it (although memory remains allocated). The value of the outer variable remains unchanged, i.e. it does not point at the allocated memory afterward. Compilers will sometimes issue a warning that a local variable masks another variable of the same name.

The second line initialises a variable declared at a broader scope.