How to hide the inherited TObject constructor while the class has overloaded ones?
Take a look at this class:
TTest = class(TObject) public constructor Create(A:Integer);overload; constructor Create(A,B:Integer);overload; end;
Now when we want to use the class:
var test: TTest; begin test:= TTest.Create; //this constructor is still visible and usable! end;
Can anyone help me with hiding this constructor?
So long as you have overloaded constructors named
Create, you cannot hide the parameterless
TObject constructor when deriving from
This is discussed here: http://www.yanniel.info/2011/08/hide-tobject-create-constructor-delphi.html
If you are prepared to put another class between your class and
TObject you can use Andy Hausladen's trick:
TNoParameterlessContructorObject = class(TObject) strict private constructor Create; end; TTest = class(TNoParameterlessContructorObject) public constructor Create(A:Integer);overload; constructor Create(A,B:Integer);overload; end;
Delphi: How to overload the parent constructor in a descendant but , I have introduced a 'superbase' class if you like with a hidden constructor, but hides the contructor private constructor Create; reintroduce; // this can Create; begin inherited Create; // does no extra work! and there is a public parameterless constructor for TObject, from which all objects are descended. Interesting, need to try this one day to see how well this works so the base constructor TObject.Create cannot be called.[WayBack] delphi - How to hide the inherited TObject constructor while the class has overloaded ones? - Stack Overflow --jeroen
You can hide the inherited Create by just introducing a non overloaded Create. As you need two overloaded Create, you can either merge those into one Create with an optional second parameter:
TTest = class(TObject) public constructor Create(A:Integer; B: Integer = 0); end;
This will give a compiler warning, signalling that you're hiding the default parameterless constructor. To get rid of the warning you can declare the hiding constructor like so:
TTest = class(TObject) public constructor Create(A:Integer; B: Integer = 0); reintroduce; end;
or, if this is not feasible, you can introduce an intermediate class introducing the first create and then the final class with the overloaded second one:
preTest = class(TObject) public constructor Create(A:Integer); reintroduce; end; TTest = class(preTest) public constructor Create(A,B:Integer);overload; end;
Inheritance — What your mother never told you, C++ FAQ, When my base class's constructor calls a virtual function on its this object, why What's the meaning of, Warning: Derived::f(char) hides Base::f(double) ? be virtual : if the derived class doesn't like the behavior in one of them, it doesn't have to call that Improving this via the Public Overloaded Non-Virtuals Call Protected� The Wiert Corner – Interesting, need to try this one day to see how well this works so the base constructor TObject.Create cannot be called.[WayBack] delphi – How to hide the inherited TObject constructor while the class has overloaded ones? – Stack Overflow –jeroen
Another option is to use the
deprecated keyword and
raise an exception at runtime.
TTest = class(TObject) public constructor Create; overload; deprecated 'Parameterless constructor is not Supported for a TTest class'; constructor Create(const A: Integer); overload; constructor Create(const A, B: Integer); overload; end; implementation constructor TTest.Create; begin raise Exception.Create('Parameterless constructor is not Supported for a TTest class.'); end;
Class inheritance, Class inheritance is a way for one class to extend another class. Object of Rabbit class have access to both Rabbit methods, such as rabbit.hide() , and also to Its prototype, that is Rabbit.prototype (has hide , but not run ). So a derived constructor must call super in order to execute its parent (base)� 2 How to hide the inherited TObject constructor while the class has overloaded ones? Feb 11. 1 How to check if a TCP port is available with Delphi? Feb 13.
Overriding and Hiding Methods (The Java™ Tutorials > Learning the , The ability of a subclass to override a method allows a class to inherit from a When overriding a method, you might want to use the @Override annotation that method in the superclass, then the method in the subclass hides the one in the Such overloaded methods neither hide nor override the superclass instance� Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super class but it does not inherit constructor of super class because of following reasons:
Only a subclass object object is created that has super class variables. This situation is different from a normal assumption that a constructor call means an object of the class is created, so we can’t blindly say that whenever a class constructor is executed, object of that class is created or not.
- Why do you want to? What are you trying to achieve?
- +1. I thought this had been asked about before, but the only question I found wasn't expressed nearly as succinctly as this.
- @Marjan Venema:It's obvious that I want to make a standard class, and my class shouldn't have this default constructor.
- I tend not to fight the language/framework I am working with. Just declare it with reintroduce (effectively hiding the default one). Have it throw an exception and/or mark it deprecated? If the compiler accepts the deprecated on it, then you will get compile time warnings and otherwise run time exceptions. But I see David has just expanded his answer with some interesting ideas.
- Deprecated is quite nice. You can get the compiler to block on that.
- Is there something special about Create or TObject, or does this rule generalize to all overloaded methods with the same name as non-overloaded, non-virtual base methods?
- It is general. Any overloaded method in a derived class (and that is every class) automatically makes the inherited method overloaded, too.
- @Rob So far as I can tell, there's nothing special about constructors,
TObject. The same behaviour can be reproduces with ordinary methods.
- Note that you can still do: inherited Create; in descendant classes.
- Ok, I'm a bit late to the discussion, but the first code snippet will not cause a warning, because
TObject.Createis not virtual or dynamic. That is also why
reintroducehas no effect and is not needed at all.