4 ms·
I think this approach is working too hard to realize Virtual Tables and Virtual Pointers. Another, different implementation is to define an array of function po
by mistrial9 5y ago
I think this approach is working too hard to realize Virtual Tables and Virtual Pointers. Another, different implementation is to define an array of function pointers, one function per method implemented, and then extend the array per subclass; each child class fills in its own or parents function pointers at init time.
- potiuper 5y agoWhat would also be helpful is a standard way to declare to the compiler that it should or should not optimize each flagged polymorphic late bound call implementation to be early bound; moving the calls into separate or merged files during linking is not an in context solution.
- mistrial9 5y agonot really important, retrieval of a function pointer at a known slot, and then calling that function pointer, can be really fast on a lot of arch.
- jhgb 5y agoOr perhaps go the more novel way of Piumarta's and Warth's Open, extensible object models? http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.121.6603&rep=rep1&type=pdf http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.121...
- sligor 5y agoNot an array, a struct is ok, and in C99 style it is very well readable. For example Linux Kernel style is: static const struct file_operations fops = { .open = my_open, .release = my_release, .read = my_read, .write = my_write }; that's all, very straightforward. Also there is no need to prefix the function name with &
- ncmncm 5y agoThis pattern goes back to pre-Version-7 UNIX kernels, with only minor syntactic updates. That it long predates OO demonstrates it is not OO. That does not make it any less effective, or less useful.
- adrian_b 5y agoIt does not predate OO. The first OO language, SIMULA-67 became public knowledge before the start of even the PDP-7 UNIX, which is extremely unlikely to have used such a pattern, which probably appeared only when UNIX was rewritten in C, starting in 1973. Even Smalltalk-72 predated the C-version of UNIX. However, it is likely that this pattern was chosen in UNIX independently of the previous OO languages.
- pjmlp 5y agoAlso what many usually forget, C++ was also born on the same building as UNIX. Hence why C++ got so fast adopted across UNIXes and C compiler vendors on other platforms.
- ncmncm 5y agoThat was not the reason. But it was the reason that C, as standardized, adopted several C++ innovations first.
- pjmlp 5y agoIt was definitly the reason, by 1992 it was impossible to buy a C compiler that also did not had a C++ one in the box. Also why Apple moved from Object Pascal to C++ on MPW.
- ncmncm 5y agoNone of those C or C++ compilers came from Bell Labs, or were on UNIX tapes from Bell Labs. I used Cfront on Apollo machines in the '80s. Effectively nobody was using Cfront by 1992. Apple, particularly, was not strongly influenced by Bell Labs or UNIX. Apple A/UX–a UNIX for Macintosh, a niche product from that period–shipped without a C++ compiler. MPW in 1992 shipped without a C++ compiler.
- mistrial9 5y agoPP (parent post) here - yes, several variations available in C99; I haven't tried to retrieve my examples yet. (I used this commercially in the 90s)