Richard R wrote: > Hello, I've developed a lot of custom controls that work pretty well however > I dont understand the use of Inherited or what it really does. I'm guessing > it allows polymorphic ability so other controls can be made based on > whatever I make.
Actually, "inherited" is just for inheritance; it isn't necessary for polymorphism, or vice versa. > I've derived all my controls from TCustomControl except for > one that is a TGraphic. I guess what I'm asking is, when I override a > procedure from the derived control, do I need to include inherited all the > time? You don't strictly need to, but it's usually a good idea. The "inherited" keyword is used to bypass the language's usual dynamic-dispatch mechanism. Normally, when you call a virtual (or dynamic) method, the actual code that gets invoked depends on the run-time type of the object you're calling it on. This allows descendant classes to override methods introduced in ancestor classes. However, it also means that you don't have any compile-time control over exactly which method gets called. It's always determined at run time. That restriction makes it difficult for inheritance to work, since a descendant class can't simply "extend" a method of its ancestor; it has no facility to call the inherited method. The object the method would get called on is Self, and the run-time type of Self remains the same, so trying to invoke the inherited method would only lead to infinite recursion. This is why Delphi has a special keyword, inherited. It tells the compiler not to generate a virtual method call. Instead, it calls the inherited method directly. > I also need to know what difference it makes if i place Inherited > before or after code, The difference is the same as the placement of any other function call. Put the call to the inherited method at the place where you need the code in the inherited method to run. > and also what's the difference between calling > Inherited by itself and Inherited Procedurename. When the parameter list of the inherited method is not the same as the method in which you're calling it, you need to specify the name of the method, along with the parameters. You must use the name of the method if the method is a function and you need the return value of that call. If the inherited method is virtual, and if the descendant method is overriding that method, then you typically won't need the name of the method; the parameter lists will be the same. > In VC++ I'm pretty sure > it's the same thing as calling CClassName::ProcedureName in overrides. Yes, except Delphi will figure out the base class for you, instead of having you specify it everywhere you need to call the base class's methods. -- Rob __________________________________________________ Delphi-Talk mailing list -> [email protected] http://www.elists.org/mailman/listinfo/delphi-talk
