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 8654581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:49:25+00:00 2026-06-12T14:49:25+00:00

Given the following class hierarchy: TClass1 = class end; TClass2a = class(TClass1) end; TClass2b

  • 0

Given the following class hierarchy:

TClass1 = class
end;

TClass2a = class(TClass1)
end;

TClass2b = class(TClass1)
end;

I operate on them using the following overloaded procedures

procedure DoSomething(AObj : TClass1); overload;
begin
  // ...Do something for TClass1
end

procedure DoSomething(AObj : TClass2a); overload;
begin
  // Do something as parent class
  DoSomething(TClass1(AObj))

  // ...Do something for TClass2a
end

procedure DoSomething(AObj : TClass2b); overload;
begin
  // Do something as parent class
  DoSomething(TClass1(AObj))

  // ...Do something for TClass2b
end

How can I dynamically cast each parameter to its parent class, instead of hard coding it?
I’d like to replace this:

// Do something as parent class
DoSomething(TClass1(AObj))

With something more generic, like this

// Do something as parent class
DoSomething(AObj.ClassParent(AObj))

Update: DoSomething procedures must reside outside the class hierarchy in this case. I can’t reverse the structure, so I can’t take advantage of class inheritance and polymorphism.
Furthermore, this is just an example. I’d like answers to focus on the core question: how can I cast an object to its parent class at runtime.

  • 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-06-12T14:49:26+00:00Added an answer on June 12, 2026 at 2:49 pm

    overload is only useful at compile-time, it allows the compiler to select the most appropriate method based on the type of the object that’s passed as a parameter. You can’t use the overload mechanism at run time to dynamically make the call because, by that time, the code has been compiled and the one overloaded procedure already chosen. For all you know the proper method to call (based on “overloaded” logic) might not even be available: If the compiler never chose the overloaded method for anything, the linker might have discarded. Because of this you can’t use RTTI, because the method might simply NOT EXIST in the executable, unless you already hard-coded a call to it.

    Your only choice is to do some hard coding. I’d create a method that takes two parameters, the object to operate on and a TClass parameter, something like this:

    procedure Dispatcher(Obj: TClass1);
    var AsClass: TClass;
    begin
      AsClass := Obj.ClassType;
      while Assigned(AsClass) do
      begin
        // Hard-coded dispatch for the type in AsClass.
        if AsClass.InheritsFrom(TClass3) then
          DoSomething(TClass3(Obj))
        else if AsClass.InheritsFrom(TClass2) then
          DoSomething(TClass2(Obj))
        else if AsClass.InheritsFrom(TClass1) then
          DoSomething(TClass1(Obj));
    
        // This emulates the "inherited" call in normal polymorphic OOP.
        // We're simply recursively calling the dispatcher for the parent of AsClass.
        AsClass := AsClass.ClassParent;
      end;
    end;
    

    Given an object of type TClass3, this procedure will call DoSomething 3 times, once for each level of inheritance. And it will choose the proper overloaded version because of the hard-coded cast.

    Sample code:

    var X1: TClass1;
    begin
      X1 := TClass3.Create;
      Dispatcher(X1); // This will call all 3 versions of DoSomething, in order.
    end;
    

    Since the code doesn’t really use the overloaded keyword for anything useful, I’d drop it’s use, give distinct names to all methods so the code in the Dispatcher method looks like this:

    procedure Dispatcher(Obj: Tobject);
    var AsClass: TClass;
    begin
      AsClass := Obj.ClassType;
      if AsClass.InheritsFrom(TClass3) then
        DoSomething_Class3(TClass3(Obj))
      else if AsClass.InheritsFrom(TClass2) then
        DoSomething_Class2(TClass2(Obj))
      else if AsClass.InheritsFrom(TClass1) then
        DoSomething_Class1(TClass1(Obj));
    
      if AsClass.ClassParent <> nil then
        Dispatcher(Obj, AsClass.ClassParent);
    end;
    

    This variant is safer in the long-run because it doesn’t depend on compiler-magic. For example, in the first variant, if you decide to drop the overloaded procedure that works for a parameter of type TClass2 but you’d forget to drop the call with the TClass2() cast in the Dispatcher, you’d get two calls for the overloaded method that takes the TClass1 parameter, because that would be the best match for the now useles:

    DoSomething(TClass2(Obj))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following hierarchy class Content < ActiveRecord::Base end class Page < Content end
Given is the following class hierarchy: Class Diagram http://img535.imageshack.us/img535/4802/personusermanager.jpg Additional info: The Person class
Given the following class, used to build a tree hierarchy: public class simpletest {
Given the following class hierarchy, I would like Foo to be serialized differently depending
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following models: class Post(models.Model): title = models.CharField(max_length=200) html = models.TextField() class PostTag(models.Model):
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
I want to override the tree_id field as following: Given: class Thing(MPTTModel): thing_id =
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following code: final class retVal { int photo_id; } Gson gson =

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.