What's the difference between how virtual and non-virtual member functions are called?

前端 未结 3 525
囚心锁ツ
囚心锁ツ 2021-01-17 01:13
#include
using namespace std;
int main(){
    class c1{

        public:
         int func(){
            cout<<\"in the  c1\";
         }

            


        
3条回答
  •  别那么骄傲
    2021-01-17 01:39

    When a member function is not virtual, the function called is determined only by the type of the expression to the left of the dot (.) or arrow (->) operator. This is called the "static type".

    When a member function is virtual, the function called is determined by the actual most derived type of the object named by the expression left of the dot (.) or pointed to by the expression left of the arrow (->). This is called the "dynamic type".

    Note that when a variable, member, parameter, or return type used to the left of a dot has a plain class type, the static type and dynamic type are always the same. But if a variable, member, parameter, or return type is a pointer or reference to a class type, the static type and dynamic type can be different.

提交回复
热议问题