How the V-Table mechanism works?
Answers were Sorted based on User's Feedback
Answer / dee
V-table is created for class when a class has atleast one
virtual function and each virtual function has a entry in
the V-table.
| Is This Answer Correct ? | 48 Yes | 3 No |
Answer / nishikant sahu
V-Table holds the addresses of virtual functions of a class
and type_info structure for RTTI.V-table created class basis.
when an object created compiler secretly inserted the
v-table pointer of the respective class and then Vptr
iterator accordingly. all are done under the hood.
| Is This Answer Correct ? | 21 Yes | 3 No |
Answer / ramesh
V-table is specific to class. No matter how many objects
are created for this class, only one virtual table exists.
Each virtual function in the class will have a pointer in
Vtable.
Whenever a code like Base *b = new Derived; is encountered,
during compile time the search will happen in Base class.
Its only during run time the actual objects are invoked and
the fucntion present in derived class will be called.
| Is This Answer Correct ? | 6 Yes | 1 No |
Answer / deepa balamurugan
If the both Base and the Derived class having the same
function then the Base class funtion should be declared as
virtual function. Then the C++ determines which function to
be called at the runtime based on the type of the object
pointed to by the Base pointer rather than the type of the
pointer.
As soon as the compiler reads the keyword virtual ,it will
create the vtable contains the address of the virtual
function inside that class and as soon as vtable is
created ,vptr is initialized to the proper vtable.
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / tiru
virtual table contains the address of the virtual functions
and pointers to the objects of the each of the derived
classes.
| Is This Answer Correct ? | 6 Yes | 12 No |
Answer / achal
V-Table mechanism works on principle of dynamic binding
i.e. which version of the function has to be called is
decided by CPU at run time.
Use of virtual functions make the execution slower. Not all
the objects of the same class have different V-Table.
| Is This Answer Correct ? | 2 Yes | 10 No |
What is a wchar_t in c++?
When volatile can be used?
how can i access a direct (absolute, not the offset) memory address? here is what i tried: wrote a program that ask's for an address from the user, creates a FAR pointer to that adress and shows it. then the user can increment/decrement the value in that address by pressing p(inc+) and m(dec-). NOW, i compiled that program and opened it twice (in 2 different windows) and gave twice the same address to it. now look what happen - if i change the value in one "window" of the program, it DOES NOT change in the other! even if they point to the same address in the memory! here is the code snippet: //------------------------------------------------------ #include <stdio.h> //INCLUDE EVERY KNOWN HEADER FILE #include <conio.h> //FOR ANY CASE... #include <iostream.h> #include <dos.h> #include <process.h> main() { int far *ptr; //FAR POINTER!!! long address; char key=0; //A KEY FROM THE KEYBOARD int temp=0; clrscr(); cout<<"Enter Address:"; cin>>hex>>address; //GETS THE ADDRESS clrscr(); (long)ptr=address; temp=*ptr; //PUTS THE ADDRESS IN THE PTR cout<<"["<<hex<<(unsigned long)ptr<<"]="<<*ptr<<" = "<< (char)(*ptr); //SHOWS: [address]=value=ASCII symbol. while (key!=27) //WHILE YOU DONT PRESS ESC. { while(!kbhit()) //WHILE KEY IS NOT PRESSED { if (temp!=*ptr) { temp=*ptr; clrscr(); cout<<"["<<hex<< (unsigned long)ptr<<"]="<<*ptr<<" = "<<(char)(*ptr); }; //IF THE VALUE HAS CHANGED, CLEAR THE SCREEN AND SHOW //AGAIN if (key=='p') {key=0; (*ptr)++; } //INCREMENT VALUE if (key=='m') {key=0; (*ptr)--; } //DEC. VALUE }; key=getch(); //IF A KEY IS PRESSED, READ IT FROM THE //KEYBOARD }; return 0; //IF ESC WAS THE KEY, EXIT THE PROGRAM } //---------------------------------------------------------
Write a code/algo to find the frequency of each element in an array?
Who made c++?
Is it possible for the objects to read and write themselves?
What is a virtual destructor?
What is a block in c++?
What is size_type?
How would you obtain segment and offset addresses from a far address of a memory location?
What is friend class in c++ with example?
What is meant by forward referencing and when should it be used?