Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!hc!lll-winken!uunet!mcvax!ukc!pyrltd!slxsys!jclark!jjc From: jjc@jclark.UUCP (James Clark) Newsgroups: comp.lang.c++ Subject: Re: address of virtual function (revisited) Message-ID: Date: 11 May 89 09:14:28 GMT References: <904@garya.Solbourne.COM> <6590114@hplsla.HP.COM> Sender: jjc@jclark.UUCP Distribution: comp Organization: James Clark, London Lines: 50 In-reply-to: jima@hplsla.HP.COM's message of 5 May 89 17:46:43 GMT In article <6590114@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes: Well, I find the cost somewhat discouraging if all one is trying to do is return a value from an object of uncertain class. For example, try to find an inexpensive way to get a hierarchy of classes to return information about their sizes -- presumably: class baseclass { virtual int size(){return sizeof(baseclass);} }; class derivedclass : baseclass { int size(){return sizeof(derivedclass);} } For these simple get-a-value, put-a-value type virtual access functions one could hope for code that compiles to a simple double indirection: baseclassptr->size(); compiles to: baseclassptr->(vtable+offset_of_size)->a_constant_equal_to_size; or maybe even: baseclassptr->vtable[offset_of_size_constant] One way to handle this might be to extend the language to support virtual static const data members: virtual in that accesses to them go through the vtable, static in that they are per class rather than per instance. Then one could say: struct baseclass { virtual static const int size = sizeof(baseclass); }; struct derivedclass : baseclass { static const int size = sizeof(derivedclass); }; One would be able to access such a member like any normal data member of the class. James Clark jjc@jclark.uucp