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?
The code you shown is valid unless
returns a pointer to an local object, which would result in an Undefined Behavior & might show the behavior that you experience.
Something like:
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->imgC1is always accessible and should be updated in the member function the way you are using it.