Different ways of implementing virtual functions by a C++ compiler.

C

Chetan Raj

Hi,

What are the different ways in which a virtual function can be
implemented by the C++ compiler? I know VTABLE and VPOINTER is only one
of the way. What are the other ways? What are the pros and cons of
these different ways? Why is VTABLE and VPOINTER method, the most
preffered method?

Thanks,
Chetan Raj


PS: I know that these questions would have been asked earlier in the
group, but my search led to ways in which a programmer should or should
not implement vitrtual function in the derived class. If you can give
me pointer to the source where this is already discussed, it wil be
helpful.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
O

osmium

Chetan Raj said:
What are the different ways in which a virtual function can be
implemented by the C++ compiler? I know VTABLE and VPOINTER is only one
of the way. What are the other ways? What are the pros and cons of
these different ways? Why is VTABLE and VPOINTER method, the most
preffered method?

Thanks,
Chetan Raj


PS: I know that these questions would have been asked earlier in the
group, but my search led to ways in which a programmer should or should
not implement vitrtual function in the derived class. If you can give
me pointer to the source where this is already discussed, it wil be
helpful.

I suggest you post to comp.compilers, it is active. This group is not about
"How to write a compiler".
 
A

Alf P. Steinbach

* Chetan Raj:
What are the different ways in which a virtual function can be
implemented by the C++ compiler? I know VTABLE and VPOINTER is only one
of the way. What are the other ways?

Here's one way you can think of a virtual function call in the context
of single inheritance (each class inherits at most from one base class,
which in turn, and so on):

first, if the class that is the object's dynamic type has an
implementation of that function, that implementation is executed;
otherwise, the same check is performed on that class' base class, and
so on.

That's suggests an obvious implementation strategy, which IIRC was
actually used by Borland's C++ compiler for a language extension used to
designate Windows window message event handlers.

It was also how virtual function call ("method calls") were originally
described and sometimes implemented in the Smalltalk language.

What are the pros and cons of
these different ways?

Memory, execution speed, flexibility.

Why is VTABLE and VPOINTER method, the most preffered method?

It works well in practice. ;-)

No, that's not a tongue-in-cheek answer.

It's the literal truth.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top