[Insight-users] Re: about object factory

Luis Ibanez luis.ibanez at kitware.com
Sat Apr 23 17:08:40 EDT 2005


Hi WanLin,

The New() method is implemented as a Macro in the file

          Insight/Code/Common/itkMacros.h

It first checks is there is any registered factory that
knows how to build a particular object.

This is done with the code :

   x *rawPtr = ::itk::ObjectFactory<x>::Create();

in line 327.



Most of the time, objects will not be created by a factory,
however this line gives the opportunity to chech for such
eventuality.



When there is no registered factory that claims to know how
to build and object, then the Macro proceeds to line 330:

           if(rawPtr == NULL)
             {
             rawPtr = new x;
             }


where the object is dynamically created by the
typical "new" operator.


In other words, most of the time, the only thing that you
have to do is to put the itkNewMacro() in your class.


Only in very few exceptional cases you would have to worry
about the Factories.



    Regards,


       Luis



---------------
wlzhu wrote:

> Hi, luis:
> I am beginner of ITK. When i use it, i think i should know some 
> fundamental principle of itk. A problem about object factory which 
> confused me one week. When create a new object of derived class 
> from itk::LightObject. Static function New() was called, but when i 
> follow the code, I find that the task of create the object should be 
> done by virtual function CreateObject() declared 
> in CreateObjectFunctionBase and implemented in its derived class. The 
> implementation  of function CreateObject() in class CreateObjectFactory 
> only called T::New(). It just like a loop. So I guess every class should 
> define such a CreateObject() function, However I didn't find it. Could 
> you please tell me Where the CreateObject is really implemented? Thank you.
>  
> Luck
>  
>  
> WanLin





More information about the Insight-users mailing list