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

  • Home
  • SEARCH
  • 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 6109927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:27:32+00:00 2026-05-23T14:27:32+00:00

This is probably a simple question, but i would like to know how to

  • 0

This is probably a simple question, but i would like to know how to ensure a class’ constructor is called.

If i have the following code:

type TMyObject = class(TObject)
  public
     constructor Create;override;
end;

implementation 

constructor TMyObject.Create;override;
begin
  inherited;
  //do other instantiation
end;

Delphi does not allow this – ‘Cannot override a static method’.

What i would like to do is ensure that the object is created using my custom Create constructor AND prohibiting calling the ancestors Create constructor.

My current solution to the problem is to define a uniquely signatured Create constructor like so:

constructor Create(aName : String);overload;

but the programmer could potentially call the ancestors Create() method.

  • 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-23T14:27:32+00:00Added an answer on May 23, 2026 at 2:27 pm

    You simply re-introduce a constructor with the ancestor’s name. Once you do that, there’s no way for the user to create a TMyObject calling the constructor introduced in TObject. If you use code like this:

    TMyObject = class
    public
      constructor Create;
    end;
    
    constructor TMyObject.Create;
    begin
      // I am not calling the inherited constructor because
      // I do not want to.
    end;
    

    You don’t use the override modifier on TMyObject.Create because the ancestor’s constructor is not virtual.

    Using this scheme it is impossible for the user to create your TMyObject using a constructor introduced in an ancestor. In this case, the ancestor is TObject, and the only constructor it has is TObject.Create. If the user writes this code:

    X := TMyObject.Create;
    

    it’s quite obvious, TMyObject‘s constructor would be called, not the one introduced in TObject.


    If you’re afraid users would jump through hoops in order to create your class using ancestor’s constructor, you can do your stuff from the AfterConstruction method. That’s a virtual method, so it gets called even if your object is created using a class reference of an ancestor’s type:

    TMyObject = class
    public
      procedure AfterConstruction;override;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really simple question but... I would like to do a
I know this is probably a very simple question but how would I do
There is probably is simple fix for this but I currently have code similar
This is probably a simple question, but how do I know when a Storyboard
This is probably a simple question, but I can't figure it out. I would
This is probably a simple question that I am just missing but I have
I know this is probably a simple question but my mind just isn't working
This is probably an overly simple question, but I have a blog Creating Wealth
This is probably a simple question but why in the following does the paragraph
This is probably a simple question but I am not an ASP.NET developer and

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.