CodificandoBits
CodificandoBits

Reputation: 970

Scope of class member within conditional if

In C++ I've created a class with a protected (data type) member and with a private (function) member:

class Qt3DViewer : public QMainWindow
{
 ...
 protected:
    vtkImageData * imgC1;
 ...
 private:
    void ComputeSlices();
}

In the implementation of the class member "ComputeSlices" I'm using a conditional if to update imgC1:

void Qt3DViewer::ComputeObliqueSlices()
{
 ...
 if (someFlag==1)
     this->imgC1 = reader1->GetOutput();
 else
     this->imgC1 = reader2->GetOutput();

 ...
 // Code that requires this->imgC1 updated!
}

The problem is that updating of imgC1is valid only within the conditional, after the if block updating goes out of scope and imgC1 member is NOT really updated!

How can I update imgC1using a conditional block and ensure that after the block the member is actually updated?

Upvotes: 1

Views: 159

Answers (2)

Tao Feng
Tao Feng

Reputation: 89

Maybe something wrong with the reader1->GetOutput(), for example, some local object of the GetOutput() is released after the all of GetOutput()

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

The code you shown is valid unless

reader1->GetOutput(); 

returns a pointer to an local object, which would result in an Undefined Behavior & might show the behavior that you experience.

Something like:

vtkImageData *GetOutput()
{
    vtkImageData obj;
    //....
    //....
    //....
    vtkImageData* ptr = &obj;
    //....
    //....
    //....
    return ptr;
}

Note that, all class members irrespective of their access specfiers are accessible within the member functions of that class.
So unless, You have a Undefined Behavior lurking in your code, this->imgC1 is always accessible and should be updated in the member function the way you are using it.

Upvotes: 1

Related Questions