[Insight-users] arrays of ITK objects w/ protected constructors

Luis Ibanez luis . ibanez at kitware . com
Fri, 12 Jul 2002 21:33:34 -0400


Hi Suzanne,

References in C++ can only be manipulated when initialized directly.

That is, you cannot declare a "reference to float" just like:

    float & a;

Since this is as dangerous as an unitialized pointer.

References (as oposed to pointers) are supposed to be always
pointing to a valid object. (and the compiler enforces this
assumption).

A reference must be declared as:

   float   B;
   float & refToB = B;

For the same reason, It is not possible to declare an array of
references, like:

typedef float & refFloat;
refFloat  myHopeLessArray[ 100 ];

Since each one of the array elements will be an uninitialized
reference.

This, so far, is independent of wheter the constructor is
protected or not.

---

It seems that the "ugly" solution of the array of pointers
may be the best fit for what you are attempting.

A couple of "typedefs" could help a bit to reduce the ugliness...

for example:

    typedef Foo * FooPtr;
    FooPtr * manyFoos = new FooPtr[ N ];

SmartPointers could also do the trick but if you have any
performance concerns it may be better to stick with raw
pointers because every assigment on SmartPointers will
cost a Mutex operation.



    Luis


=========================================================

Suzanne Vogel wrote:

> ** Is there a way to declare an array of references (not pointers) to 
> ITK objects whose contructors are protected?
> 
> Often constructors are protected to enable creation by ObjectFactory.
> 
> Example:
> 
> template <typename foo>
> class Foo : public LightObject
> {  public:
>      /* definitions necessary for creation by ObjectFactory */
>      ...
>    protected:
>      Foo();
>      ...
> }
> 
> #include "itkFoo.h"
> void main(int argc, char **argv)
> {  Foo *array1 = new Foo[N];
>    /* array of references - does not compile */
>    /* compiler error: cannot access protected Foo constructor */
> 
>    Foo **array2 = new Foo*[N];
>    /* array of pointers - works but is ugly */
> 
>    Foo array3 = new ((Foo::New())->GetReference)[N];
>    / *attempt at array of references - does not compile */
> }
> 
> Maybe I'll compromise by making an array of SmartPointers to Foo. :(
> 
> Thanks,
> Suzanne
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
>