why base ptr cannot refer the virtual funtion in derived class

This is a discussion on why base ptr cannot refer the virtual funtion in derived class within the C++ Programming forums, part of the General Programming Boards category; I cannot compile this code. From my understanding about virtual function, the compiler should have no problem to de-referencing the ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Red face why base ptr cannot refer the virtual funtion in derived class

    I cannot compile this code.
    From my understanding about virtual function, the compiler should have no problem to de-referencing the f4 in its derived object.

    Since the virtual ptr is already in the derived object, I guess even though this object is passed to the base ptr as in the code, when de-referencing f4 in the object, the v-ptr should know the position without problem.

    currently I got compilation failed.
    Any one can explain it reasonably?

    ~/private/test>more bv.C
    Code:
    #include <iostream>
    using namespace std;
    
    class base {
    private:
    
    public:
         base() {}
         virtual void f3() { cout<<"base::f3()"<<endl;}
    };
    
    class derived: public base {
    private:
    
    public:
         derived(){}
         void f3(){cout<<"derived::f3()"<<endl;}
         virtual void f4(){cout<<"derived::f4()"<<endl;}
    };
    
    int main(){
    
    base* p = new derived;
    p->f4();
    return 0;
    }
    ~/private/test>g++ bv.C
    bv.C: In function `int main()':
    bv.C:24: error: `f4' undeclared (first use this function)
    bv.C:24: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    ~/private/test>

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    The actual type of the instance is base * and thats what the compiler sees.
    base doesnt contain a function f4 so it doesnt compile.

    ((derived *)p)->f4(); would compile but its probably not what is wanted

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    The v-ptr is used at runtime. That code still must be compiled. At compile time, how does the compiler know that f4 exists? What if someone else created another derived class and used that instead. The compiler can't tell what derived class type the instance actually has, it only knows the static type of the pointer, so it wouldn't know if that pointer really pointed to a class that had an f4 or not.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Add pure virtual f4 to the base class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base class pointer pointing at derived class
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2008, 11:26 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 08:08 AM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21