Recursive classes?
Publicado: Dom Ago 14, 2011 11:36 pm
Can a second instance of a class be created, inside the class itself,
without pointing to the same instance that was already created?
Let's say you have a form, which creates an instance of a class, and
then displays the properties for that class, on the form. So, you have
one instance of the class, so far. Call it oInstance01
Now, inside the class, there are properties which need to be processed,
by generating another instance of the same class.
But it seems as if this second instance, is merely a pointer to the
first instance, because when it's created, it seems to be populated with
the same processed values as the first instance.
For example, inside a form, you have:
*******************************************
// frmShowPropertiesOfDisAndDat
METHOD ProcessDisAndDat( oSender ) CLASS TForm1 // Button Process
Click event
....
....
Local oDisAndDatInstance := DisAndDatClass():New()
Local aSecondProperties := oDisAndDatInstance:aSecondProperties
....
oDisAndDatInstance:Process()
....
....
::edtFirstProperty:Value := oDisAndDatInstance:sFirstProperty
For nThSecondProperty := 1 To nSecondProperties
sNthSecondProperty := aSecondProperties[ nThSecondProperty ]
::cboListOfSecondProperties:AddItem( sNthSecondProperty )
Next nThSecondProperty
....
....
Return Nil
....
....
*******************************************
Now, inside the class itself, you have a string property which also
needs to be process by the same class machinery. So, inside the class,
you have
*******************************************
#include "Xailer.ch"
#include "Boole.ch"
CLASS DisAndDatClass
DATA sSolution INIT ""
DATA sFirstProperty INIT ""
DATA aSecondProperties INIT {}
....
....
METHOD SolveSecondaryEquation( nIndexOfLiteral ) CLASS DisAndDatClass
Local sSolution := ""
Local oAnotherInstance := DisAndDatClass():New()
sSolution := oAnotherInstance:sSolution
Return sSolution
....
....
*******************************************
So, the question is, will the initial instance oDisAndDatInstance point
to the same static memory space as oAnotherInstance? Or is there a
workaround for this?
without pointing to the same instance that was already created?
Let's say you have a form, which creates an instance of a class, and
then displays the properties for that class, on the form. So, you have
one instance of the class, so far. Call it oInstance01
Now, inside the class, there are properties which need to be processed,
by generating another instance of the same class.
But it seems as if this second instance, is merely a pointer to the
first instance, because when it's created, it seems to be populated with
the same processed values as the first instance.
For example, inside a form, you have:
*******************************************
// frmShowPropertiesOfDisAndDat
METHOD ProcessDisAndDat( oSender ) CLASS TForm1 // Button Process
Click event
....
....
Local oDisAndDatInstance := DisAndDatClass():New()
Local aSecondProperties := oDisAndDatInstance:aSecondProperties
....
oDisAndDatInstance:Process()
....
....
::edtFirstProperty:Value := oDisAndDatInstance:sFirstProperty
For nThSecondProperty := 1 To nSecondProperties
sNthSecondProperty := aSecondProperties[ nThSecondProperty ]
::cboListOfSecondProperties:AddItem( sNthSecondProperty )
Next nThSecondProperty
....
....
Return Nil
....
....
*******************************************
Now, inside the class itself, you have a string property which also
needs to be process by the same class machinery. So, inside the class,
you have
*******************************************
#include "Xailer.ch"
#include "Boole.ch"
CLASS DisAndDatClass
DATA sSolution INIT ""
DATA sFirstProperty INIT ""
DATA aSecondProperties INIT {}
....
....
METHOD SolveSecondaryEquation( nIndexOfLiteral ) CLASS DisAndDatClass
Local sSolution := ""
Local oAnotherInstance := DisAndDatClass():New()
sSolution := oAnotherInstance:sSolution
Return sSolution
....
....
*******************************************
So, the question is, will the initial instance oDisAndDatInstance point
to the same static memory space as oAnotherInstance? Or is there a
workaround for this?