Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overload a virtual function introduced in a parent class?

I have a parent class with one important abstract procedure which I am overloading in many child classes as the example code shown below:

TCParent = Class
private
public
 procedure SaveConfig; virtual; abstract;

end;

TCChild = Class(TCParent)
private
public
 procedure SaveConfig; override;
end;

Now there I need to (overload) this procedure with another SaveConfig procedure that will accept parameters, yet I don't want to make big changes in the parent class that might require that I go and make changes in all other child classes.

Is there a way I can overload SaveConfig in this specific child class without making big changes to the parent class and other child classes that inherit from it?

like image 544
MChan Avatar asked Dec 05 '12 01:12

MChan


People also ask

How do you overload virtual functions?

Or we could make getType() virtual in the Animal class, then create a single, separate print() function that accepts a pointer of Animal type as its argument. We can then use this single function to override the virtual function.

Can virtual method be overloaded?

Virtual methods can be overloaded in C#. Virtual methods can have both out and ref type parameters. If the derived class also has a method with the same name and signature as the base class virtual method, then the base class method will be hidden in the derived class.

How do you overload a function?

You overload a function name f by declaring more than one function with the name f in the same scope. The declarations of f must differ from each other by the types and/or the number of arguments in the argument list.

Can virtual function be overloaded in Java?

A virtual function or method also cannot be final, as the final methods also cannot be overridden. Static functions are also cannot be overridden; so, a virtual function should not be static. By default, Every non-static method in Java is a virtual function.


1 Answers

You can use reintroduce to add a new overloaded method. Note that the order of reintroduce; overload; in the child class is required; if you reverse them, the code won't compile.

TCParent = Class
private
public
 procedure SaveConfig; virtual; abstract;
end;

TCChild = Class(TCParent)
private
public
 procedure SaveConfig; overload; override;
 procedure SaveConfig(const FileName: string); reintroduce; overload;
end;

(Tested in Delphi 7, so should work in it and all later versions.)

like image 163
Ken White Avatar answered Sep 22 '22 15:09

Ken White