QT-ITK-VTK-Help
QT-ITK-VTK-Help

Reputation: 558

Alternate of typedef or right way to use typedef with if else

I have following condition:

if( type == 1)
    {
        typedef itk::Image<unsigned char, 3> itkImageType;
    itkImageType::Pointer image;
    image =Open<itkImageType>(filename);
    writeimage->Graft(image);
    }

else if(type == 2)
    {
        typedef itk::Image<unsigned char, 3> itkImageType;
    itkImageType::Pointer image;
    image =Open<itkImageType>(filename);
    writeimage->Graft(image);
    }

there are 10 cases and the stuff after typedef is common in all if- else. I want to remove this code repetition but since typedef have local scope I have to do it. Is there any way to do this?

Upvotes: 0

Views: 491

Answers (1)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248129

Just put the common stuff in a function. That's what functions are for:

template <typename PixelType>
void doStuffAndOpen() {
    //Some stuff which use Pixel Type
    Open<PixelType>(filename);
}

// and then wherever you want to do stuff with the PixelType
switch (type) {
case 1:
    doStuffAndOpen<unsigned char>();
    break;
case 2:
    doStuffAndOpen<unsigned int>();
    break;
}

Upvotes: 3

Related Questions