3

I have two classes, a base class and a derived class. My base class has a constructor of this form:

constructor TBaseClass.CreateFromXML(ANode: IXMLNode);
begin

  Create;

  //Set members from XML

end;

My derived class has a constructor of this form:

constructor TDerivedClass.Create;
begin

   FDatabaseID = -1;

end;

My problem is that when I create an object of my derived class using the constructor from the base class [ TDerivedClass.CreateFromXML(Node); ] the Create called at the beginning of the CreateFromXML constructor is not the one from my derived class, but rather the one inherited by my base class from TObject.

Is it possible to get the base class constructor to call my derived class constructor even though it's further "down" the inheritance chain?

1
  • If you need derived code to execute at a certain time during creation you should probably just use an event. That would be more elegant than trying to give the base class knowledge of the derived class. Commented Aug 4, 2010 at 12:11

1 Answer 1

9

Try declaring a constructor Create; virtual; in TBaseClass. Don't forget to mark the "derived" constructor as override.

Sign up to request clarification or add additional context in comments.

1 Comment

Just so, thank you. It's been a long time since I encountered this situation; thanks for the reminder!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.