BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tough Question About Virtual Class Methods

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical
View previous topic :: View next topic  
Author Message
Captain Jake
Guest





PostPosted: Wed May 26, 2004 4:39 am    Post subject: Tough Question About Virtual Class Methods Reply with quote



I suppose there is a better newsgroup for this, but I'm not sure which one
it is. Here is my question.

I have a set of classes that are different type of nodes for a treeview
descendent. To get a treeview descendent to create different class types you
have to set an event in TCustomTreeview, OnCreateNodeClass, that then passes
the correct class type back to the internal workings of TCustomTreeView. So
I have a class method in my base node class that looks like this for
creating children nodes of the desired type:

interface

type
TJSNNode = class(TTreeNode)
class function CreateChild(inTreeNodes: TTreeNodes; ParentNode:
TTreeNode=nil): TTreeNode; virtual;
....
end;

implementation

class function TJSNNode.CreateChild(inTreeNodes: TTreeNodes; ParentNode:
TTreeNode=nil): TTreeNode;
var tv: THTMLTreeView; tvl: THTMLTreeList;
begin
if inTreeNodes.Owner.InheritsFrom(THTMLTreeList) then
THTMLTreeList(inTreeNodes.Owner).OnCreateNodeClass := CreateNodeClass
else TTreeView(inTreeNodes.Owner).OnCreateNodeClass := CreateNodeClass;
Result := inTreeNodes.AddChild(ParentNode, '');
end;


Now, I realized quickly that there is no need to override this method in
the descendent classes, because only the CreateNodeClass method is going to
differ between my different node classes. That class method looks like this
in the base class:

interface

type
TJSNNode=class(TTreeNode)
class procedure CreateNodeClass(Sender: TCustomTreeView; var
NodeClass:
TTreeNodeClass); virtual;
...
end;


implementation

class procedure TJSNNode.CreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
begin
NodeClass := TJSNNode;
end;


This method is then appropriately overridden within the descendents so it
returns the class of the descendent in NodeClass.

So to use these classes I use something like this:

var ndsmtp: TTreeNode;

ndsmtp := TSMTPServerNode(TSMTPServerNode.CreateChild(tvlMain.Items));


The question: will this usage of TSMTPServerNode.CreateChild assign the
correct override of the CreateNodeClass class method to the
OnCreateNodeClass event handler of the treeview? If so, how does it know how
to do this? CreateChild is not overridden in the TSMTPServerNode class, only
the method it calls, CreateNodeClass, is overridden and this method is also
a class method, so it never has a "self" reference that can be used in a VMT
(indeed, I wouldn't be able to compile this code if it did).

I guess a related question would be: is there really a difference between
writing TSMTPServerNode.CreateChild and TJSNNode.CreateChild here?

--

Read Jake's Blog!
http://blogs.slcdug.org/jjacobson/Rss.aspx


Back to top
Lauchlan M
Guest





PostPosted: Wed May 26, 2004 4:52 am    Post subject: TreeViews (Re: Tough Question About Virtual Class Methods) Reply with quote



Quote:
I suppose there is a better newsgroup for this, but I'm not sure which one
it is. Here is my question.

I have a set of classes that are different type of nodes for a treeview
descendent. To get a treeview descendent to create different class types
you
have to set an event in TCustomTreeview, OnCreateNodeClass, that then
passes
the correct class type back to the internal workings of TCustomTreeView.
So
I have a class method in my base node class that looks like this for
creating children nodes of the desired type:

Out of interest, why do you need to create your own node classes for
treeview descendents? What kind of functionality are you after that you
can't readily get from standard or third party tree components?

Just interested - I think some of the third party solutions are pretty good.

Lauchlan M



Back to top
Thorsten Engler [NexusDB]
Guest





PostPosted: Wed May 26, 2004 9:04 am    Post subject: Re: Tough Question About Virtual Class Methods Reply with quote



Quote:
so it never has a
"self" reference that can be used in a VMT (indeed, I wouldn't be
able to compile this code if it did).

That's wrong. Class methods have a Self pointer just like normal methods.
For normal methods the Self pointer is a pointer to the heap where
InstanceSize bytes have been allocated for this specific instance. Self is
an instance reference. The first 4 byte of that instance memory contain the
pointer to the VMT for the exact class. That pointer to the VMT is a class
reference. Calling Object.ClassType just returns this pointer (as a TClass).
If you have a class method, Self is no longer a pointer to an instance of
the class on the heap. Instead Self is directly the class reference (in
other words Self = pointer to the VMT for that specific class).

If you look at SizeOf(TNotifyEvent) you'll see it's 8 byte. A look at the
declaration of TMethod will show that it's actually 2 pointers, Code and
Data.

Quote:
is there really a difference between
writing TSMTPServerNode.CreateChild
and TJSNNode.CreateChild here?
With the information we have now it's easy to answer this question.

Both calls will end up at the same code address. But Self (passed in the EAX
register) will be a reference to TSMTPServerNode or TJSNNode. Calls to
virtual class methods from that code will have a reference to the correct
VMT to resolve the virtual call.

--
Thorsten Engler [NexusDB Architect]



Back to top
Rudy Velthuis
Guest





PostPosted: Wed May 26, 2004 12:28 pm    Post subject: Re: Tough Question About Virtual Class Methods Reply with quote

At 06:39:10, 26.05.2004, Captain Jake wrote:

Quote:
I suppose there is a better newsgroup for this, but I'm not sure which
one it is.

It it is about the language, try b.p.d.language.delphi.general.
--
Rudy Velthuis (TeamB)

"Future historians will be able to study at the Jimmy Carter Library,
the Gerald Ford Library, the Ronald Regan Library, and the Bill
Clinton Adult Bookstore." -- George Carlin

Back to top
Rudy Velthuis
Guest





PostPosted: Wed May 26, 2004 12:36 pm    Post subject: Re: Tough Question About Virtual Class Methods Reply with quote

At 06:39:10, 26.05.2004, Captain Jake wrote:

Quote:
and this method is also a class method, so it never has a
"self" reference that can be used in a VMT (indeed, I wouldn't be able
to compile this code if it did).

Class methods also have a Self pointer, but it points to the class, and
not to an instance. And for virtual class methods, classes also have a
VMT.
--
Rudy Velthuis (TeamB)

"Future historians will be able to study at the Jimmy Carter Library,
the Gerald Ford Library, the Ronald Regan Library, and the Bill
Clinton Adult Bookstore." -- George Carlin

Back to top
John Jacobson
Guest





PostPosted: Wed May 26, 2004 5:40 pm    Post subject: Re: TreeViews (Re: Tough Question About Virtual Class Method Reply with quote

"Lauchlan M" <LMackinnon (AT) NOSPAMHotmail (DOT) com> wrote

Quote:
Out of interest, why do you need to create your own node classes for
treeview descendents? What kind of functionality are you after that you
can't readily get from standard or third party tree components?

I want polymorphic behaviour with respect to popups, display details and
click events.

Quote:

Just interested - I think some of the third party solutions are pretty
good.


I am using a third party solution: THTMLTreeList from TMS Software.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Non-Technical All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.