[Insight-users] Why classes attributes should be private in ITK?

Lemeur Yann Yann.Lemeur at stage.cnes.fr
Thu, 15 Apr 2004 09:47:29 +0200


      Hi,
Thanks for your answers!
I'm going to precise my problem: I've made a new classe which inherit from itkVectorGradientMagnitudeImageFilter. My goal was to overload the method ThreadedGenerateData() in order to include a short process on the output pixels (to be more precise:  change unwanted values of gradient). For more performance, I want to include the operation directly in the while loop of ThreadedGenerateData(). But as you can see, this method needs to access to "m_NeighborhoodRadius" and "m_RealValuedInputImage" which are private variables of itkVectorGradientMagnitudeImageFilter without accessors. So I can't use inheritance in this case!

My conclusion is that such methods (which need access to private data without accessors) become useless for inheritance mechanism. I even think that we could declare them private because we can't overload them!
Drinking litres of Colombian coffee doesn't conduct me to a solution so I think there is not...
To my mind, this case is one where private variables aren't very nice.
Is it a good reason to add Set/Get methods to this class in future versions? (even if I would prefer to have protected variables to avoid changing the parts of code I don't want to change)

Thanks

Yann  

> ----------
> De : 	Luis Ibanez[SMTP:luis.ibanez at kitware.com]
> Date :	mercredi 14 avril 2004 18:28
> A :	Lemeur Yann
> Cc :	insight-users at itk.org
> Objet :	Re: [Insight-users] Why classes attributes should be private in ITK?
> 
> 
> Hi Yann,
> 
> Mmm, You should actually we wondering why
> there shoule be *any* public or protected
> variables in ITK classes...
> 
> Encapsulation is a fundamental principle in
> Object Oriented programming. Every variable
> must be private until you prove that the design
> requires otherwise.
> 
> Note that for getting access to the variables
> most ITK classes offer Set() / Get() methods
> that are constructed with the itkSetMacro and
> itkGetMacro.
> 
> Your derived classes should simply use these
> Get/Set methods for getting access to such
> variables.
> 
> If you indicate what classes in ITK are giving
> you trouble, and what kind of variation of the
> filters you want to implement, we will be happy
> to add the Set/Get methods needed for your class
> to work correctly. That's the beauty of Open
> Source and Exterme Programming    :-)
> 
> 
> Please let us know,
> 
> 
>     Thanks
> 
> 
>       Luis
> 
> 
> 
> ------------------
> Lemeur Yann wrote:
> 
> > Hi everybody!
> > 
> > I've used ITK for about a month and I wonder why almost all classes variables are private. 
> > Actually, I have a problem when I want to create a class (class B) which inherit from a mother-class (called class A) in order to change one method (in my case: adding some lines of code to GenerateData() ).
> > If this method needs to access to class variables which are private (defined in class A), I can't reimplement it in my new class B because variables are private in the mother class A.
> > I also can't redefine the variables of A with the same name in B! 
> > 
> > The only solution I've found is to write a "sister class" which inherit from the same class as A, and which is the same as A excepted the few changes I want to add.
> > Does somebody know another solution to make it properly (ie to avoid to rewrite classes which don't change!)
> > And I also wonder what is the interest to declare all the class variables private?
> > 
> > Thanks!
> > Yann   
> > _______________________________________________
> > Insight-users mailing list
> > Insight-users at itk.org
> > http://www.itk.org/mailman/listinfo/insight-users
> > 
> 
> 
>