Jump to content
David Schwartz

Constructors + Destructors in class helpers?

Recommended Posts

I'm looking for any kind of discussion about the use of ctors and dtors within class helpers.

 

I've seen stuff that says you cannot put class ctors and dtors in them, as well as examples that DO THAT.

 

I don't want to do that; but I am trying to figure out how they get called if they're normal ctors and dtors.

 

I've read that there are other limitations of things within helpers, but ... where do I find a complete discussion of class helpers?

 

The only thing I've found on Delphi's wiki shows adding properties and methods, not ctors and dtors. SO hasn't turned up much of anything useful either.

Share this post


Link to post
48 minutes ago, David Schwartz said:

The only thing I've found on Delphi's wiki shows adding properties and methods, not ctors and dtors. SO hasn't turned up much of anything useful either.

Probably because that's the sum total of what you can do with class helpers. If you specify what you actually want to achieve, there might be a different solution.

Share this post


Link to post
Posted (edited)

Took me 10 seconds to try it out. Ctor can be added, dtor not (E2295).

 

Which makes sense if you think about it. Adding a different way to create new instances depending on if the helper is there or not is not a problem.

But not how existing instances are being destroyed.

Edited by Stefan Glienke

Share this post


Link to post
Posted (edited)

Class helpers are merely a bunch of procedures and functions grouped together that mimic methods of a class. Each method has an invisible first parameter that passes the object and treats it as if it were "self".

So literally nothing gets constructed or destructed, they're merely procedures/functions that act on an object. 

 

 

The advantage of class helpers is that one can extend a class, without actually inheriting from it, and that everything is neatly wrapped inside the namespace of that class.   As an analogy, it's a bit like having  an optional side car attached to a motor cycle, instead of permanently changing the vehicle itself.  The major disadvantage of class helpers is that there can only be one helper "in scope" for your code (so the sequence of units in your "uses" clause suddenly becomes important). 

 

 

 

(Edit) but of course a class helper can have a method that returns a new instance of the class (or of any other class), but that's not really a constructor.  It would be a factory method.

Edited by A.M. Hoornweg
  • Like 1

Share this post


Link to post
12 minutes ago, Stefan Glienke said:

Which makes sense if you think about it.

Especially as one cannot override virtual methods in a class helper.

  • Like 1

Share this post


Link to post

Thanks, guys. After some trial-and-error, I realized a helper won't do what I was wanting it to do. So instead I created a wrapper class because I needed to add data fields as well as methods to it. That seemed easier than dealing with a derived object that's part of 3rd-party lib.

Share this post


Link to post

Both class and record helpers could do with a real upgrade.  The lack of inheritance and lack of support for Generics is crippling.

  • Like 1

Share this post


Link to post
Posted (edited)
4 hours ago, Lars Fosdal said:

lack of inheritance

Inheritance of helpers is not solving the problem at all - you then still can have the case where you would want to use methods from 2 different helpers which cannot inherit from each other.

The only solution is to make not only the last visible helper usable but treat conflicting methods via overload resolution - just like extension methods in C# do.

Edited by Stefan Glienke
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×