[ITK-users] Smartpointer as a default parameter for a function

Williams, Norman K norman-k-williams at uiowa.edu
Tue Jul 29 12:52:15 EDT 2014


You do not need to explicitly initialize <itktype>::Pointer variables.
They have a default constructor that initializes the raw pointer member
m_Pointer to zero.

I would tend to use references to SmartPointers as parameters, because if
you pass a Pointer value, it creates a temporary Pointer on the stack, and
then assigns it to the member variable.  This isn¹t a huge overhead but it
does involve semaphore lock/unlock pairs protecting reference count
increment/decrement.
When you pass a Pointer by reference, it ends up doing this.

1. Construct anonymous temp Pointer.
2. assign <temp>.m_Pointer with <parameter>.m_Pointer (I.e. the actual
object pointed to by the smart pointer)
3. lock the object pointed to by <temp>.m_Pointer.
4. Increment the pointed-to object¹s reference count.
5. unlock pointed-to object.
6. Inside the method, assign <temp> to local or member variable, repeat
steps 2 through 5.
7. When the function exits, call <temp> destructor
8. lock the pointed-to object
9. decrement reference count
10. unlock pointed-to object

If you pass the smart pointer by reference, you skip creating and
destroying the temp, and only assign to a smart pointer once.

On 7/22/14, 2:37 AM, "ahmed at mufradat.com" <ahmed at mufradat.com> wrote:

>Hello All,
>
>Suppose I have class like
>
>class A{
>     public:
>     A(int a, ImageType::Pointer ptr){
>         // whatever!
>     }
>};
>
>My question is, what's the default value for that ptr? I'd like to be
>able to create an instance of that class using
>
>A myA(5);
>
>so for the definition of the constructor of A:
>
>A(int a, ImageType::Pointer ptr = WHAT?)
>
>Best regards,
>Ahmed
>_____________________________________
>Powered by www.kitware.com
>
>Visit other Kitware open-source projects at
>http://www.kitware.com/opensource/opensource.html
>
>Kitware offers ITK Training Courses, for more information visit:
>http://www.kitware.com/products/protraining.php
>
>Please keep messages on-topic and check the ITK FAQ at:
>http://www.itk.org/Wiki/ITK_FAQ
>
>Follow this link to subscribe/unsubscribe:
>http://public.kitware.com/mailman/listinfo/insight-users



________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
________________________________


More information about the Insight-users mailing list