Board index » delphi » Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)

Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)

I often hear virtual/dynamic method are better if you expect the given
method to later be overriden/redeclared....

I would think that a static method is best if you expect it to be
overridden/redeclared and a virtual method best if the opposite.
The advantage with vitual methods  (and dynamic - my understanding is that
the 2 things are the same - just that dynamic methods quesries 100% at
runtime - and doesn't look in a VTable/whatever-?) vs static methods with
the OO knowledge AFAIK which is applicable here best illustarted by this
small example.
--
Iif one have a public virtual method called "StartInit" in TMyClass1 & and
then 2 public static methods ("Init" and "Start") in TMyClass1.
Then make a decendant TMyClass2 - and you redeclare/reimplment
"Init" and "Start". if you then call MyClass2.StartInit - it'll automaticly
call the new static
methods (those 2 we just redeclared/reimplmented - "Init" and "Start") in -
MyClass2
whereas if StartInit had been static too it would have called the original
ones  in TMyClass1.
--

Or do virtual methods have something else that make neat?
I guess there may be soemthing making them neat (maybe something with memory
use.. allocation... or..?)
Does anyone know a Delphi book that is _only_ dedicated to all the possible
OO situations and OO in general.. (in that case I wouldn't hesitate to buy
it!)?

Regards
 - Thomas Schulz

 

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Hi, Thomas
  I think it is the opposite:

Quote
> Iif one have a public virtual method called "StartInit" in TMyClass1 & and
> then 2 public static methods ("Init" and "Start") in TMyClass1.
> Then make a decendant TMyClass2 - and you redeclare/reimplment
> "Init" and "Start". if you then call MyClass2.StartInit - it'll
automaticly
> call the new static
> methods (those 2 we just redeclared/reimplmented - "Init" and "Start")
in -
> MyClass2
> whereas if StartInit had been static too it would have called the original
> ones  in TMyClass1.
> --

In the case you say, if you call TMyClass2.StartInit, the methods of
TMyClass1 will be called.
One of the uses of virtual methods is that the calling function can be
'aware' of new versions of those methods.

In your example, Init and Start should be protected virtual methods, and
StartInit, a public static method.

Regards
  Cristian

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Hi, about a good book that covers, this, I don't know of a Delphi-based
book.
In my opinion, you have to read Bjarne Stroustroup's book on C++.

Regards,
  Cristian

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
> In the case you say, if you call TMyClass2.StartInit, the methods of
> TMyClass1 will be called.
> One of the uses of virtual methods is that the calling function can be
> 'aware' of new versions of those methods.

That's oppesite of what I've read a couple of places...
E.g. in Borland C++ 3 book about OO they write that
an inherited virtual method can find the new static methods while
an inherited static method will call the ones in its original class.
And since StartInit is the one that call the 2 other static methods
reffering to my example - one should think it would be better to do that
virtual
and then redeclare the static ones (because their implementation needed to
be changed for accomodate the needs in the new decendant class).
The user of the 2 components could call StartInit on both methods (and
without anything done to it except original declared as virtual it would
call the right implementations of Start and Init).
But you say that's incorrect? I can of course go doublecheck and give
pagenumbers... I guess..

-regards
Thomas Schulz

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
> But you say that's incorrect? I can of course go doublecheck and give
> pagenumbers... I guess..

I guess it maybe is... Just reading in it now (page 160-163)...
I can't understand though how calling a static_ method can handle-to-resolve
finding the correct method in a decending class (I mean.. it's early
binding - that confuses me)...  It would give more sense if it was a virtual
method that at runtime figured out what the right methods to call would
be...
I guess I always though that it was the "caller" that should be "virtual"
and not the "called"... (I guess my initial example also described 100% how
I thought it was..)

Allright... If this is really true.... How does it do it then?
How can something with binding at compiletime figure out to call the right
virtual methods runtime??
I guess it's just the opposite of what I though all along... I completely
misunderstood... mmm.. oh well..
A good thing though is that other stuff might give more meaning now...
(allthough I am still confused with the conceptual names given to the 2
types of methods..)

Thomas

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
> Hi, about a good book that covers, this, I don't know of a Delphi-based
> book.
> In my opinion, you have to read Bjarne Stroustroup's book on C++.

but isn't it old? :)
OK does anyone have an ISDN (isn't that what they are called) or full title?
(I guess I can buy it though places like amazon.com - it's not out of age?
:).
Thomas

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
Thomas Schulz wrote...
>OK does anyone have an ISDN (isn't that what they are called) or full title?

ISBN. ISDN is a hardware protocol (I have a 64k ISDN telephone line).
--
Rudy Velthuis (Team JEDI)
http://delphi-jedi.org

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
> ISBN. ISDN is a hardware protocol (I have a 64k ISDN telephone line).

<grin>
yeah yeah -  I knew :) - but same acronym can have different meanings
but you are of course right - ISBN - thanks :)

Thomas

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Quote
> How can something with binding at compiletime figure out to call the right
> virtual methods runtime??

OK - I partially_ found out how -
I  guess I originally just had understood it reversely so to speak <grin>
Thomas

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


When a method is defined as virtual, the compiler doesn't calculate a static
address for then entry point of the procedure. Instead, it puts an entry
into what is called the virtual table for the class. When the method is
called, even from a static function, what happens is that instead of jumping
directly to a memory address, a search is done for the 'real' address of the
function. How this is done? It checks for the virtual table of the instance
for which it's been called, i.e., it can be a descendant class, so it is
more time consuming that a static method, but this makes possible to alter
the behaviour of functions that are called on base classes.
In fact, there are classes that have 'pure' virtual methods(sorry, for
object pascal I must say abstract method), that allow to have a class that
cannot be instantiated. Instead, you must provide an implementation for the
method in the descendant classes you need to instance.

Regards
  Cristian

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Hi, Thomas
  I bought my first copy by 1990.
  A couple years ago, I bought a newer release. It is much more modern, but
it is a very theoretical book. It is not a recipe book, it teaches you to
think oop.

Regards
  Cristian
"Thomas Schulz" <tho...@REMeVE.micro-sys.dk> escribi en el mensaje
news:8sv84f$4201@bornews.borland.com...

Quote
> > Hi, about a good book that covers, this, I don't know of a Delphi-based
> > book.
> > In my opinion, you have to read Bjarne Stroustroup's book on C++.

> but isn't it old? :)
> OK does anyone have an ISDN (isn't that what they are called) or full
title?
> (I guess I can buy it though places like amazon.com - it's not out of age?
> :).
> Thomas

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Oh! I forgot. I bought it on amazon.

Cristian

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Although I already have a reasonable knowledge of polymorphism, I still found
your lucid explanation helpful - thanks!

Regards,

--
John

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
GXExplorer (The Explorer Replacement) - www.gxexplorer.org
Includes free Delphi Windows Explorer components.
JTSoftware - www.jtsoftware.co.uk
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Quote
> When a method is defined as virtual, the compiler doesn't calculate a

static...

(Snipped)

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Cristian Ormazabal - DX-Squad wrote...

Quote
>In fact, there are classes that have 'pure' virtual methods(sorry, for
>object pascal I must say abstract method), that allow to have a class that
>cannot be instantiated.

Contrary to C++, in Object Pascal, it can be instantiated, but you will
be given a warning about instantiating a class containing abstract
methods.
--
Rudy Velthuis (Team JEDI)
http://delphi-jedi.org

Re:Static Methods vs Virtual Methods (with methods you expect to be overridden/redeclared)


Do C++ compilers actually refuse to compile code instantiating an abstract
class?

--
John

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
GXExplorer (The Explorer Replacement) - www.gxexplorer.org
Includes free Delphi Windows Explorer components.
JTSoftware - www.jtsoftware.co.uk
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Quote
> Contrary to C++, in Object Pascal, it can be instantiated, but you will
> be given a warning about instantiating a class containing abstract
> methods.
> --
> Rudy Velthuis (Team JEDI)
> http://delphi-jedi.org

Go to page: [1] [2]

Other Threads