Q:
How virtual functions are implemented in C++?
belongs to collection: C++ Interview Questions For Experienced
C++ Interview Questions For Experienced
- Can a constructor throw an exception? How to handle the error when the constructor fails?
- What is the initializer list in C++?
- When do we use the Initializer List in C++?
- What is a copy constructor in c++?
- When are copy constructors called in C++?
- Why copy constructor take the parameter as a reference in C++?
- Why copy constructor argument should be const in C++?
- Can one constructor of a class call another constructor of the same class to initialize this object?
- Can a copy constructor accept an object of the same class as a parameter, in place of reference of the object? If No, why not possible?
- Are Constructors and destructors can declare as const?
- Can we make a copy constructor private in c++?
- Can you explain the order of execution in the constructor initialization list in c++?
- What is the conversion constructor in c++?
- What is the difference between a copy constructor and an overloaded assignment operator?
- What is the conversion operator in C++?
- When do we need to write a user-defined destructor?
- Why a class has only one destructor in c++?
- Can we have a virtual destructor in C++?
- When to use virtual destructors in c++?
- Can we have a virtual constructor in C++?
- Can you change ‘this pointer’ of an object to point to different objects in c++?
- Can you modify the ‘this pointer’ type in c++?
- Can I use realloc() on pointers allocated via new in c++?
- Why should C++ programmers minimize the use of ‘new’?
- Can I free() pointers allocated with new in c++?
- Is there any problem with the following c++ code : char*a=NULL, char& p = *a?
- Can I delete pointers allocated with malloc() ijn c++?
- How to call a non-const member function from a const member function in C++?
- When should I use references, and when should I use pointers?
- How virtual functions are implemented in C++?
- Is there a separate vtable for each object?
- Can virtual functions be inlined in c++?
- Can a virtual function is called inside a non-virtual function in C++?
- What is a pure virtual function in C++?
- What is difference between Virtual function and Pure virtual function in C++?
- Why is a pure virtual function initialized by 0 in c++?
- Can we access private data members of a class without using a member or a friend function?
- Can virtual functions be private in C++?
- What is an abstract class in c++?
- Write down some important points related to abstract function in c++?
- What is the difference between a concrete class and an abstract class in c++?
- What is a template function in c++?
- What is the difference between function overloading and templates in c++?
- Can we combine C and C++ code?
- How can I include a non-system C header file in my C++ code?
- What is the effect of extern “C” in C++?
- Why do C++ compilers need name mangling?
- What is typecasting in c++?
- When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?
- How does the compilation/linking process work in c++?
- How to make a C++ class whose objects can only be dynamically allocated?
- What does the explicit keyword mean?
- How do you access the static member of a class in c++?
- Distinguish between shallow copy and deep copy in c++?
- Friend class and function in C++?
- What is the Diamond problem? How can we get around it in c++?
- Why virtual functions cannot be static in C++?
- What is the “mutable” keyword in C++?
- How to handle the exception in C++?
- What is a Memory Leak?
- Why static functions cannot access non-static variables in c++?
- What is a dangling pointer in c++?
- STL Containers – What are the types of STL containers?
- What are the functions of the scope resolution operator in c++?
- Write a program that describes the safe way to access one object to another in C++?
- Could you write an example code that describes the use of explicit keyword in c++
- Why is “using namespace std;” considered bad practice in c++?
- Why can templates only be implemented in the header file in c++?
- Do all virtual functions need to be implemented in derived classes in c++?
- Do all pure virtual functions need to be implemented in derived classes in c++?
- How to call a parent class function from a derived class function in c++?
- How to access members of the namespace in different files?
- How to convert a std::string to const char* or char* in c++?
Answer:
Virtual functions are implemented using a table of function pointers, called the VTABLE. There is one entry in the table per virtual function in the class. This table stores the address of the virtual function and it is created by the constructor of the class.
The object of the class containing the virtual function contains a virtual pointer (vptr) that points to the base address of the virtual table in memory. Whenever there is a virtual function call, the v-table is used to resolve the function address.
Due to the vptr, the size of the object increases by the size of the pointer. The vptr contains the base address of the virtual table in memory. Note that virtual tables are class-specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains.
At the time when a virtual function is called on an object, the vptr of that object provides the base address of the virtual table for that class in memory. This table is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.
Note: You should not call the virtual function in the constructor. Because the vtable entries for the object may not have been set up by the derived class constructor yet, so you might end up calling base class implementations of those virtual functions.
need an explanation for this answer? contact us directly to get an explanation for this answer