[Insight-users] ITK SmartPointers and my own class.

Luis Ibanez luis.ibanez at kitware.com
Fri Apr 16 07:59:28 EDT 2010


Hi Peter,

This is a very interesting (and very useful) question.

You already pointed out some of the critical points,
so let me just expand in the following list:

For a new class to be able to use ITK SmartPointers
you should:


1)  Derive from the itk::LightObject

2) Implement the New() operator.
    This can be done by simply using the
    itkNewMacro( Self ),
    and of course, declaring the "Self" type.

3) Have a protected default constructor
    (constructor without arguments)

4) Have a protected destructor

5) Have a private declaration of the
    copy constructor (and not implement it)

6) Have a private declaration of the
    operator=(), (and not implement it)



Note that items (3,4,5,6) are not really "requirements"
for getting the SmartPointer to work, they are actually
intended to prevent users from misusing the class.
For example, they make impossible for you to use the
"new" operator, and therefore enforce the use of New().

Note that the class doesn't have to be put inside the
"itk" namespace (although you could do it, if you find
it useful).


Here is the minimal example of a class that
uses ITK Smart Pointers:

--------------------------------------------------------
#include "itkLightObject.h"
#include "itkObjectFactory.h"

class MyClass : public itk::LightObject
{
public:
  typedef MyClass Self;
  typedef itk::SmartPointer< Self >   Pointer;
  typedef itk::SmartPointer< const Self > ConstPointer;

  itkNewMacro( Self );

protected:
  MyClass() {}
  virtual ~MyClass() {}

private:
  MyClass( const Self & ); // not implemented
  const Self & operator=( const Self & ); // not implemented
};


int main()
{
  MyClass::Pointer foo = MyClass::New();
  return 0;
}
--------------------------------------------------------



        Regards,


                Luis


-----------------------------------------------------------------------------
On Thu, Apr 15, 2010 at 8:25 PM, Pete79 <pkoniusz at hotmail.com> wrote:

>
> Dear all.
>
> Can you give me a simple example how to wrap my own class to provide it
> with
> Smart Pointer functionality? So far I've gathered I really need to inherit
> from the LightObject class, what next?
>
> Let's say I have got the following:
> class MyClass: public LightObject
> {
> private:
> int member;
> public:
> MyClass(int memberC) : member(memberC) {}
> ~MyClass() { do something; }
> };
>
> How can I wrap it up with SmartPointer?
>
> typedef typename SmartPointer<MyClass> MyClassSPT;
>
> What else?
> When I instantiate it with:
> MyClassSPT aaa=MyClass::New() it won't work fine I guess coz the
> constructor
> of MyClass takes a parameter as its argument.
>
> Can you give me the tips and snippets of the code which show how to deal
> with it?
>
> Many thanks,
> Peter
>
>
> --
> View this message in context:
> http://old.nabble.com/ITK-SmartPointers-and-my-own-class.-tp28261973p28261973.html
> Sent from the ITK - Users mailing list archive at Nabble.com.
>
> _____________________________________
> 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.html
>
> 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://www.itk.org/mailman/listinfo/insight-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100416/4f89accd/attachment.htm>


More information about the Insight-users mailing list