Could anyone help me clarify the concept of polymorphism? [duplicate] - c++

How does Virtual Method Invocation work in C++?

Through virtual tables.
Read this article, http://en.wikipedia.org/wiki/Virtual_table.
I could explain it here, but the wikipedia does a better job than I could.

The C++ standard doesn't specify how the virtual function mechanism should be implemented.
That said, I think all current C++ compilers use virtual tables.
The common way to do this for classes which contain at least one virtual function to have a hidden pointer to a so-called virtual table, where the addresses of the virtual functions for a specific class are entered in compiler-specific order.
Each constructor will then set this hidden pointer to the virtual table of the class it belongs to.

Every class with at least one virtual method has it's virtual table - table of pointers to functions that are that class's methods.
It's extensively used in COM.

With VTables and function pointers. Virtual functions' function pointer will be listed in VTable
MFC is using Message Map instead of Virtual function, which reduces the size limitation. If we use several virtual function VTable will end up with big size.

Related

In C++, what does the v-table do [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
why do I need virtual table?
What is a vtable in C++?
So far I know that vtable is a virtual table which has an array of pointers to virtual functions. Is there an article I can read with an example of a practical implementation? (Any walk through will be appreciated.)
V-tables (or virtual tables) are how most C++ implementations do polymorphism. For each concrete implementation of a class, there is a table of function pointers to all the virtual methods. A pointer to this table (called the virtual table) exists as a data member in all the objects. When one calls a virtual method, we lookup the object's v-table and call the appropriate derived class method.
vTable (virtual table) is an implementation detail of dynamic dispatch (virtual methods).
See C++-Lite-Faq for more details.
For all it's worth, it is not a standard C++ terminology. It is just an implementation detail used by the implementation to implement virtual functions/dynamic binding

What is going on behind the scenes with polymorphism in C++? [duplicate]

How does Virtual Method Invocation work in C++?
Through virtual tables.
Read this article, http://en.wikipedia.org/wiki/Virtual_table.
I could explain it here, but the wikipedia does a better job than I could.
The C++ standard doesn't specify how the virtual function mechanism should be implemented.
That said, I think all current C++ compilers use virtual tables.
The common way to do this for classes which contain at least one virtual function to have a hidden pointer to a so-called virtual table, where the addresses of the virtual functions for a specific class are entered in compiler-specific order.
Each constructor will then set this hidden pointer to the virtual table of the class it belongs to.
Every class with at least one virtual method has it's virtual table - table of pointers to functions that are that class's methods.
It's extensively used in COM.
With VTables and function pointers. Virtual functions' function pointer will be listed in VTable
MFC is using Message Map instead of Virtual function, which reduces the size limitation. If we use several virtual function VTable will end up with big size.

virtual functions when there no inheritance involved

is there any need for virtual when no inheritance is involved? I think that virtual function or keywork is tightly coupled with inheritance as per my level of understanding and knowledge. Am I right? Is there any place where virtual function comes to use other than with inheritance.(Base and derived classes)?
Yes, you are right.
Even more: virtual functions are only needed for runtime polymorphism, which is only a part of what inheritance is about.
no, you are right, there is no use of virtual functions outside of inheritance, because virtual functions are specifically made in order to allow derived class to "override" base class functions (oftenly extending them by calling them and then do additional treatment)
Virtual is used only where runtime polymorphism is needed. Use of virtual make sure that correct version (BASE/DERIVED) of the method is getting called and the call is resolved at runtime according the type of caller object. For more refer to Virtual Functions
and YES, your understanding is correct.

How does virtual method invocation work in C++?

How does Virtual Method Invocation work in C++?
Through virtual tables.
Read this article, http://en.wikipedia.org/wiki/Virtual_table.
I could explain it here, but the wikipedia does a better job than I could.
The C++ standard doesn't specify how the virtual function mechanism should be implemented.
That said, I think all current C++ compilers use virtual tables.
The common way to do this for classes which contain at least one virtual function to have a hidden pointer to a so-called virtual table, where the addresses of the virtual functions for a specific class are entered in compiler-specific order.
Each constructor will then set this hidden pointer to the virtual table of the class it belongs to.
Every class with at least one virtual method has it's virtual table - table of pointers to functions that are that class's methods.
It's extensively used in COM.
With VTables and function pointers. Virtual functions' function pointer will be listed in VTable
MFC is using Message Map instead of Virtual function, which reduces the size limitation. If we use several virtual function VTable will end up with big size.

What is a vtable in C++ [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
why do I need virtual table?
What is a vtable in C++?
So far I know that vtable is a virtual table which has an array of pointers to virtual functions. Is there an article I can read with an example of a practical implementation? (Any walk through will be appreciated.)
V-tables (or virtual tables) are how most C++ implementations do polymorphism. For each concrete implementation of a class, there is a table of function pointers to all the virtual methods. A pointer to this table (called the virtual table) exists as a data member in all the objects. When one calls a virtual method, we lookup the object's v-table and call the appropriate derived class method.
vTable (virtual table) is an implementation detail of dynamic dispatch (virtual methods).
See C++-Lite-Faq for more details.
For all it's worth, it is not a standard C++ terminology. It is just an implementation detail used by the implementation to implement virtual functions/dynamic binding