C + + practice - omission and access rights of virtual function keywords

Posted by ruraldev on Sun, 02 Jan 2022 07:27:18 +0100

The description of virtual functions is as follows:

1. Because virtual functions only call objects of classes, global or static functions cannot be declared as virtual functions.

2. You can use the virtual keyword to declare in a derived class that this is a virtual function that overrides a function of the base class, but this is not necessary; Because after the virtual function of the base class is covered by a subclass, the function of the subclass must be a virtual function.

3. Virtual functions in the base class must be defined unless they are pure virtual functions declared with pure specifier. (for more information about pure virtual functions, see abstract classes).

4. The virtual function calling mechanism can specify which class of function to call by explicitly limiting the function name by using the range resolution operator (::).

5. When defining a virtual function of a class outside the class, you cannot add the virtual keyword, otherwise an error will be reported during compilation.

6. The access permission of the virtual function is only determined by the declaration type of the called object.

7. Virtual function calls are judged according to whether the declared type of the object is a virtual function, not according to the actual object type.

An example of the second point:

#include <cstdio>

class A {

public:

  virtual void func(){printf("func A.\n");}

};



class B: public A {

public:

  void func(){printf("func B.\n");}

};



int main()

{

  A a;

  B b;

  A * p;



  p = &a;

  p->func();



  p = &b;

  p->func();



  return 0;

}



Output:

func A.

func B.

Although the subclass does not use the virtual keyword, it is still a virtual function.

The fourth example:

#include <cstdio>

class A {

public:

  virtual void func(){printf("func A.\n");}

};



class B: public A {

public:

  void func(){printf("func B.\n");}

};



int main()

{

  A a;

  B b;

  A * p;



  p = &a;

  p->A::func();



  p = &b;

  p->A::func();



  return 0;

}



Output:

func A.

func A.

If you specify the call directly, it is no longer related to virtual functions.

Instead, directly access the member functions or member variables of this class or its parent class according to the declaration type of the current object. And external access, of course, requires public permission.

Example of the fifth point:

class A{

public:

  virtual void func();



};



// virtual void A::func() will report an error

void A::func()

{



}

Define its member function outside the class, and cannot add the virtual keyword, otherwise the compilation will report an error.

Example of point 6:

#include <cstdio>

class A {

public:

   virtual void func(){printf("func A.\n");}

};





class B: public A {

private:

  virtual void func(){printf("func B.\n");}

};





int main()

{

  A a;

  B b;

  A * p;





  p = &a;

  p->func();





  p = &b;

  p->func();





  return 0;

}



Output:

func A.

func B.

The virtual function of the subclass has private access permission. When called, it can still be accessed public ly according to the access rules of the parent class.

Example of point 7:

#include <cstdio>



class A {

public:

   void func(){printf("func A.\n");}

};



class B: public A {

public:

  virtual void func(){printf("func B.\n");}

};



class C: public B {

public:

  virtual void func(){printf("func C.\n");}

};



int main()

{

  A a;

  B b;

  C c;

  A * pa;

  B * pb;



  pa = &a;

  pa->func();



  pa = &b;

  pa->func();



  pa = &c;

  pa->func();



  pb = &b;

  pb->func();



  pb = &c;

  pb->func();



  return 0;

}



Output:

func A.

func A.

func A.

func B.

func C.

When using a pointer to a base class without a virtual function, even if its subclass overrides the function as a virtual function, it will be called according to the rules of the base class rather than the virtual function.

Relevant abstracts:

Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual.

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.

Virtual functions in a base class must be defined unless they are declared using the pure-specifier. (For more information about pure virtual functions, see Abstract Classes.)

The virtual function-call mechanism can be suppressed by explicitly qualifying the function name using the scope-resolution operator (::). Consider the earlier example involving the Account class. 

Remember that, when you are trying to give implementation of a virtual member function, you cannot mark virtual again outside the class definition.

reference resources:

Virtual Functions | Microsoft Docshttps://docs.microsoft.com/en-us/cpp/cpp/virtual-functions?view=msvc-170

Topics: C++ Programming