Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 969425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:38:18+00:00 2026-05-16T02:38:18+00:00

I’ve read the official documentation and I understand what class references are but I

  • 0

I’ve read the official documentation and I understand what class references are but I fail to see when and why they are best solution compared to alternatives.

The example cited in the documentation is TCollection which can be instantiated with any descendant of TCollectionItem. The justification for using a class reference is that it allows you to invoke class methods whose type is unknown at compile time (I assume this is the compile time of TCollection). I’m just not seeing how using TCollectionItemClass as an argument is superior to using TCollectionItem. TCollection would still be able to hold any descendant of TCollectionItem and still be able to invoke any method declared in TCollectionItem. Wouldn’t it?

Compare this with a generic collection. TObjectList appears to offer much the same functionality as TCollection with the added benefit of type safety. You are freed from the requirement to inherit from TCollectionItem in order to store your object type and you can make a collection as type specific as you want. And if you need to access item’s members from within the collection you can use generic constraints. Other than the fact that class references are available to programmers prior to Delphi 2009 is there any other compelling reason to use them over generic containers?

The other example given in the documentation is passing a class reference to a function that acts as an object factory. In this case a factory for creating objects of type TControl. Its not really apparent but I’m assuming the TControl factory is invoking the constructor of the descendant type passed to it rather than the constructor of TControl. If this is the case then I’m starting to see at least some reason for using class references.

So I guess what I’m really trying to understand is when and where class references are most appropriate and what do they buy a developer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:38:18+00:00Added an answer on May 16, 2026 at 2:38 am

    MetaClasses and "class procedures"

    MetaClasses are all about "class procedures". Starting with a basic class:

    type
      TAlgorithm = class
      public
        class procedure DoSomething;virtual;
      end;
    

    Because DoSomething is a class procedure we can call it without an instance of TAlgorithm (it behaves like any other global procedure). We can do this:

    TAlgorithm.DoSomething; // this is perfectly valid and doesn't require an instance of TAlgorithm
    

    Once we’ve got this setup we might create some alternative algorithms, all sharing some bits and pieces of the base algorithm. Like this:

    type
      TAlgorithm = class
      protected
        class procedure DoSomethingThatAllDescendentsNeedToDo;
      public
        class procedure DoSomething;virtual;
      end;
    
      TAlgorithmA = class(TAlgorithm)
      public
        class procedure DoSomething;override;
      end;
    
      TAlgorithmB = class(TAlgorithm)
      public
        class procedure DoSomething;override;
      end;
    

    We’ve now got one base class and two descendent classes. The following code is perfectly valid because we declared the methods as "class" methods:

    TAlgorithm.DoSomething;
    TAlgorithmA.DoSomething;
    TAlgorithmB.DoSomething;
    

    Let’s introduce the class of type:

    type
      TAlgorithmClass = class of TAlgorithm;
    
    procedure Test;
    var X:TAlgorithmClass; // This holds a reference to the CLASS, not a instance of the CLASS!
    begin
      X := TAlgorithm; // Valid because TAlgorithmClass is supposed to be a "class of TAlgorithm"
      X := TAlgorithmA; // Also valid because TAlgorithmA is an TAlgorithm!
      X := TAlgorithmB;
    end;
    

    TAlgorithmClass is a data type that can be used like any other data type, it can be stored in a variable, passed as a parameter to a function. In other words we can do this:

    procedure CrunchSomeData(Algo:TAlgorithmClass);
    begin
      Algo.DoSomething;
    end;
    
    CrunchSomeData(TAlgorithmA);
    

    In this example the procedure CrunchSomeData can use any variation of the algorithm as long as it’s an descendant of TAlgorithm.

    Here’s an example of how this behavior may be used in a real-world application: Imagine a payroll-type application, where some numbers need to be calculated according to an algorithm that’s defined by Law. It’s conceivable this algorithm will change in time, because the Law is some times changed. Our application needs to calculate salaries for both the current year (using the up-to-date calculator) and for other years, using older versions of the algorithm. Here’s how things might look like:

    // Algorithm definition
    TTaxDeductionCalculator = class
    public
      class function ComputeTaxDeduction(Something, SomeOtherThing, ThisOtherThing):Currency;virtual;
    end;
    
    // Algorithm "factory"
    function GetTaxDeductionCalculator(Year:Integer):TTaxDeductionCalculator;
    begin
      case Year of
        2001: Result := TTaxDeductionCalculator_2001;
        2006: Result := TTaxDeductionCalculator_2006;
        else Result := TTaxDeductionCalculator_2010;
      end;
    end;
    
    // And we'd use it from some other complex algorithm
    procedure Compute;
    begin
      Taxes := (NetSalary - GetTaxDeductionCalculator(Year).ComputeTaxDeduction(...)) * 0.16;
    end;
    

    Virtual Constructors

    A Delphi Constructor works just like a "class function"; If we have a metaclass, and the metaclass knows about an virtual constructor, we’re able to create instances of descendant types. This is used by TCollection’s IDE Editor to create new items when you hit the "Add" button. All TCollection needs to get this working is a MetaClass for a TCollectionItem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.