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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:11:24+00:00 2026-05-17T23:11:24+00:00

I have CObject as main class and CRock, CDesk, CComputer as derivates from CObject.

  • 0

I have CObject as main class and CRock, CDesk, CComputer as derivates from CObject. I would like to write a function that reads a class enumeration (integer probably like OBJECT_COMPUTER) and returns the specific type.

Example:

function createObject( iType : Integer ) : CObject;
begin
  case iType of
    OBJECT_ROCK : Result := CRock.Create();
    OBJECT_DESK : Result := CDesk.Create(); 
  end;
end;

so I can use it like this: myRock := createObject( OBJECT_ROCK );

Now my problem is that the object returned is the main class parent and I can’t use Rock functions on ‘myRock’ without type casting ‘createObject( OBJECT_ROCK )’ from CObject to CRock and I don’t want to have 3 functions for each sub-class. Any ideas? Thanks in advance.

  • 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-17T23:11:25+00:00Added an answer on May 17, 2026 at 11:11 pm

    If I understood correct, you’d declare a skeleton of derived functionality on the base class with abstract methods, then override and implement the method in each derived class.

    type
      CObject = class
        procedure DoIt; virtual; abstract;
      end;
      CRock = class(CObject)
        procedure DoIt; override;
      end;
      CDesk = class(CObject)
        procedure DoIt; override;
      end;
    
    
    var
      myRock: CObject;
    begin
      myRock := createObject(OBJECT_ROCK);
      myRock.DoIt;
      myRock.Free;
    end;
    

    In the above example, ‘DoIt’ call on the ‘myRock’ instance would be correctly resolved to the method of that class.

    If this is relevant at all read about abstract methods here.

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

Sidebar

Related Questions

I have a silly question. I would like to know if there is performance
Suppose I would like to extend class C , getting SubC . Now I
I have an object with which I would like to make follow a bezier
I have a mapping like this <class name=UserFileSummary table=UserFile lazy=false> <id name=FileId column=UserFileId type=int>
Have had to write my first proper multithreaded coded recently, and realised just how
I have a tempated base class check and publically derived class childcheck. the base
I have some Objective-C++ code that I'm trying to get events out of an
I have the following code: public void BeginConvert(object data) { ConverterData cObject = (ConverterData)data;
I have just converted a project from Visual Studio 2003 to 2005 and although
I have JSON data like shown below. I only included column names once to

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.