Using new to create a subclassed FolderItem creates 2 objects?

on TOF see:
https://forum.xojo.com/t/to-subclass-or-add-property-to-new-class/76361/1

Either i don’t understand subclassing in Xojo, or Tim Hare is wrong, or subclassing folder item is a special case…

basically he is saying that if you subclass FolderItem and institute the class a new instance of teh subclass is created but an instance of the superclass is also created and it’s properties are copied the the subclass instance and the the vanilla FolderItem instance goes out of scope…

Is he right? That seems wrong to how I think of subclasses working…

-Karen

say you get a folder item from an API
thats one object
if you now have a subclass that needs to be created from that folder item then you end up calling that subclass constructor
thats two objects
IF your subclass then calls the copy constructor of the superclass (via super.constructor) this doesnt create yet another instance - it basically tells the superclass to initialize itself using whatever folder item was passed in
It doesnt cat yet another instance - Constructors are JUST methods - Initializers
They do not cause memory to be allocated (vastly different than something like C++)
They just run code to set the state of an object - but they ARE called whenever you use the NEW operator
However, you can call them any old time (really)

Unfortunately folder item subclasses are often of limited utility because you need to pass folder items to Xojo API’s, and you cannot get instances of your subclass from Xojo api’s without constructing instance from the folder item Xojo apis return

you can ease some of the burden with operator_convert
You can have an OPERATOR convert that turns instances of your subclass into a folder item (this is the CONVERT TO form)

And you can use the CONVERT FROM form to make it so assigning a folder item TO a variable that is your subclass type juts works too

something like

   Class MyFolderItemSubclass 
         inherits folderitem

   function operator_convert() as Folderitem
          return Foldertitem(me) // since I'm a subclass this casts me UP to a folder item
   end function

   sub operator_convert(f as folderitem)
        // note this acts like a constructor 
        // so if you need to do some special initialization then do so
        // or call the constructor you have that does that
        self.constructor(f)
   end sub

   sub constructor(f as folderitem)
          // whatever special code I need to initialize my subclass extra properties

          super.constructor(f)
   end sub

and this should let you do something like


Dim mfs As MyFolderItemSubclass = SpecialFolder.UserHome
Dim p As String = mfs.NativePath
Break

Dim f As folderitem = mfs.child("Desktop")
p = f.NativePath

so you could convert back and forth easily

NOTE this is written off the top of my head here in the forum without ANY checks or trials but it seems about right

That is not the case I was thinking of… Of course that way one would need a copy constructor as your are starting with a FolderItem base class.

I was thinking of the case where one is using the new operator to create a new instance of the folderitem subclass. I thought that was what was being discussed on the TOF.

In that case (I think) there is only one instance… I thought Tim was saying that the call to Super.Constructor internally created a new instance which who’s properties got copied to the subclass instance. I did not think that was or should be happening. The call to Super.Constructor is just an initializer. The subclass instance then should be able to be used everywhere one could use a Folderitem in the Xojo framework.

If I misread the discussion and it was about initially getting a folderitem from an Xojo API then I owe Tim an apology!

But I the I realize Xojo may be doing some funny business under the hood for some things … Way back when for example IIRC one had to do things like
Dim P as Picture = NewPicture(1,1,1)
To get a usable picture and not just using the new operator. I don’t remember if Folderitem was like that.

-Karen

Yes in this case there is only one instance
The call to super.constructor in that case is just the initializer call so the super class initializer runs & does whatever it does
It DOES NOT create a new instance

Correct - it DOES NOT create a new instance
Constructors ARE NOT ALLOCATORS - they do not create instances
Only a call to NEW does that