[Insight-users] itk::SmartPointer - problem making code const-correct

Frederic Perez fredericpcx at gmail.com
Mon May 31 04:53:02 EDT 2010


Hello Luis,

On Fri, May 28, 2010 at 5:35 PM, Luis Ibanez <luis.ibanez at kitware.com>wrote:

>
> Hi Frederic,
>
> The function:
>
>      template<typename TITKImgType>
>      bool
>      WriteFile(
>          const std::string& a_fullFilename,
>          typename TITKImgType::ConstPointer);
>
> will be better written as
>
>      template<typename TITKImgType>
>      bool
>      WriteFile(
>          const std::string& a_fullFilename,
>          const TITKImgType * );
>
>
> and you will be able to call it as
>
>     ImgType::Pointer image = filter->GetOutput();
>
>     WriteFile<ImgType>("out.mhd", image );
>
>
> (not need to use "GetPointer()" in this case)
>
> Normally you don't need to call GetPointer() in ITK.
>

Thank you very much, that works fine.

Following your indications, I've just checked it works as expected with this
simple code, for completeness:

#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageRegionIterator.h>

typedef short PxlType;
typedef itk::Image<PxlType, 3> ImgType;

void
TryInspectors(const ImgType* a_img) {
  const ImgType::IndexType idx = { 0, 0, 0 };
  a_img->GetPixel(idx);

  const ImgType::RegionType region = a_img->GetLargestPossibleRegion();
  itk::ImageRegionConstIterator<ImgType> it(a_img, region);

  // The following mutating methods cause compiler error, as expected:
  a_img->SetPixel(idx, 666); // <- discards qualifiers
  itk::ImageRegionIterator<ImgType> it(a_img, region); // <- invalid
conversion
}

int main(int argc, char** argv) {
  if (argc < 2)
    return -1;

  typedef itk::ImageFileReader<ImgType> ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(argv[1]);
  reader->Update();

  ImgType::ConstPointer image = reader->GetOutput();
  TryInspectors(image);

  return 0;
}

Time to get rid of GetPointer() calls in our code!

Kind regards,

Frederic Perez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100531/e9fe3900/attachment-0001.htm>


More information about the Insight-users mailing list