Thread: Queries regarding virtual pointer and virtual function

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Queries regarding virtual pointer and virtual function

    1: If the base class has a virtual function and
    then the derived class also has function under the same name, is it possible that even the derived class will have a virtual table despite the common named function of derived class being non-virtual? (Asked because in a lot of examples it has been taken as virtual even in derived class)

    2: Does the v_ptr (virtual pointer) added into the BASE class by compiler added as public so that it can be inherited by derived class and it can point to the v_table of that derived class?
    Last edited by gaurav#; 12-28-2016 at 03:12 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) If the derived member is declared non-virtual, then the derived function hides the base function instead. Since it's not virtual, the class is not virtual, so it needs no virtual function table. EDIT: Actually, a derived function with the same signature as the base is always going to be virtual if the base is. I just remembered this now. If the signature is slightly different (e.g. different return type), then it would hide the base function and would not be virtual unless declared virtual.
    2) It's not public in the sense that the derived class can read it. The vptr table is an implementation detail that only the compiler knows about. So it does make sense for derived class, or any class for that matter, to be able to read it.
    Last edited by Elysia; 12-28-2016 at 04:44 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    1) If you declare a function virtual in the base, it is virtual in the derived whether that is declared or not. In the case of non-virtual functions Elysia is right that the derived function would hide the base function.
    2) there is little need to go delving into vptr tables. That's an implementation detail for the compiler, not the programmer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with virtual function and virtual inheritance
    By shaxquan in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2010, 10:39 AM
  2. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  3. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  4. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  5. Confusing between Virtual and Non-virtual function
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2006, 06:30 PM

Tags for this Thread