#include <iostream>

class Base {

    int b_value;
    void my_func() {std::cout << "This is Base's non-virutal my_func()" << std::endl; }

    virtual void my_Vfunc() {std::cout << "This is Base's virutal my_Vfunc()" << std::endl;}


class Derived: public Base {

    int d_value;
    void my_func() {std::cout << "This is Derived's non-virtual my_func()" << std::endl; }

    virtual void my_Vfunc() {std::cout << "This is Derived's virtual my_Vfunc()" << std::endl;}

int main(){

Base * base = new Derived;

return 0; 

enter image description here

I was trying to understand the internal of virtual functions. So far I understand that upcasting the derived class to base class still calls the virtual function when we do base->my_Vfunc() because of Derived::vfptr.

My question is how does base's my_func() gets called here? My main confusion is how does (during the upcasting) the derived class object provide information about the base class non virtual function since it only has information of base as Base::b_value.

  • 1
    Why? Because you didn't write virtual my_Func in the base class. That's what virtual means. Or are you really asking, "How can the computer tell what I wrote in the base class?" Feb 3, 2016 at 8:25
  • Yes, I think I should ask how rather then why.
    – solti
    Feb 3, 2016 at 8:28
  • I had hoped that this question was about slicing on assignment, but it seems that you've conflated pointers and objects. Feb 3, 2016 at 8:38
  • hmm not assignment really ... I just wanted to know how internally the upcasting works.
    – solti
    Feb 3, 2016 at 8:46

1 Answer 1


Non-virtual functions are called based on the type that the compiler sees. If you have a Base* variable, then calling non-virtual functions will call the Base functions. That's the difference between virtual and non-virtual; calling a virtual function always calls the function that is appropriate for the object.

And you are not assigning an object. You are assigning an object pointer. If you assigned an object (for which you would have to implement an assignment operator), then both virtual and non-virtual would call the Base function, because the object would be a Base object.

  • Thank you gnasher729 for the answer .. this makes sense now.
    – solti
    Feb 3, 2016 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.