<br>Hi Stuart,<br><br><br>Thanks for your detailed question.<br><br><br>1) When writing functions that take an image as an argument,<br> we usually use a Raw pointer instead of a Smart Pointer.<br><br> The reason is that SmartPointers will not do polymorphism.<br>
<br> That is, if you use smart pointers, you can call that function<br> later with an argument that is a derived class of that image <br> type.<br><br> If you look at most ITK filters, you will find that raw pointers<br>
are used in the API, for passing and receiving images.<br><br> However, when we call those functions we pass a SmartPointer<br> to them (since SmartPointers know how to cast themselves as<br> raw pointers).<br>
<br><br>2) The only case in which you may want to pass an image<br> SmartPointer as argument to a function is when you are<br> creating that image inside the function. <br><br> In that case, passing the SmartPointer by reference is<br>
a reasonable choice.<br><br><br>3) Please note that the construction<br><br> const Image::ConstPointer & ptr ....;<br><br> prevents the internal mechanisms of the SmartPointer <br> from working, since the "const" keyword prevents the "ptr"<br>
variable from changing. (strictly speaking it prevents the<br> smart pointer from calling the non-const method Register()<br> on the object that it points to).<br><br><br><br> Regards,<br><br><br> Luis<br>
<br><div class="gmail_quote"><br>-------------------------------------------------------------------------<br>On Thu, May 27, 2010 at 1:21 PM, Stuart Golodetz <span dir="ltr"><<a href="mailto:itk@gxstudios.net">itk@gxstudios.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000">
<div style="font-family: -moz-fixed; font-size: 13px;" lang="x-western">
<pre>Hi,
Hope this is the right place to post this. I was just wondering if
there's a reason why itk::SmartPointer was designed so as not to allow e.g.
itk::Image<int,3>::Pointer image;
const itk::Image<int,3>::ConstPointer& cimage = image;
?
The equivalent with boost::shared_ptr is allowed, e.g.
boost::shared_ptr<int> p(new int);
const boost::shared_ptr<const int>& cp = p;
This doesn't seem like a major problem, until you start writing
functions taking const itk::Image<...>::ConstPointer& parameters - at
which point it won't let you pass a normal Pointer in without explicitly
constructing a ConstPointer from it. Now the types are often quite long,
and it's annoying to have to add extra typedefs in the calling code just
for that purpose. Duplicating the functions with const
itk::Image<...>::Pointer& parameters doesn't work either, because you
get a combinatorial explosion when you have multiple such parameters.
For instance, with 3 parameters, you have to create functions with
combinations:
const Pointer&, const Pointer&, const Pointer&
const Pointer&, const Pointer&, const constPointer&
const Pointer&, const ConstPointer& const Pointer&
// more here
const ConstPointer&, const ConstPointer& const ConstPointer&
This seems like an unproductive way to spend one's time, to say the
least. The only other "reasonable" alternative I've managed to come up
with that doesn't either (a) clutter up the call site or (b) cause the
combinatorial explosion just outlined, is to just use the non-const
Pointers everywhere and abandon the idea of making the code
const-correct. But that seems defeatist to me <span><span> :) </span></span> Please could you tell
me if there's something I'm missing? (And if so, what?)
Cheers,
Stuart
</pre>
</div>
</div>
<br>_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.html" target="_blank">http://www.kitware.com/products/protraining.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
<br></blockquote></div><br>