C++ conversion methods between base class Pointers and derived class Pointers

  • 2020-06-15 09:56:41
  • OfStack

Function overloading, function hiding, function overwriting

Function overloading occurs only in the same scope (or in the same class), with the same name but different argument types or number of arguments. Function overloads cannot be distinguished by the return type of the function, because we do not know the return type of the function until it returns.

Function hiding and function overwriting occur only between base and derived classes.

Function hiding means that a function in a derived class has the same name as a function in the base class, but the function is not defined as a virtual function in the base class, which is the case of function hiding.

Hidden means that when a derived class object accesses this function using a regular call method, the derived class object first accesses the function in the derived class. The function in the base class is hidden from the derived class object. But hiding doesn't mean it's non-existent or completely inaccessible. Through the b - > Base::func() accesses hidden functions in the base class.

Function override refers to a polymorphic phenomenon caused by a virtual function defined in a base class. A member function declared as virtual in a base class and redefined in one or more derived classes. Polymorphism is achieved by accessing an overridden member function of the derived class with the same name through a base class pointer or reference to the derived class.

Conditions for function coverage:

1: Member functions in the base class are declared as virtual functions by the virtual keyword; 2: The function in the derived class must be exactly 1 with the name, parameter type and number of the function in the base class; 3: Assign derived class objects to base class Pointers or references to achieve polymorphism.

Functional overlay (polymorphism) implements a method of accessing (different) derived classes from a base class. We call this the reverse of the base class.

A conversion between a base class pointer and a derived class pointer

1. Base class pointer to base class object and derived class pointer to derived class object

This is a common situation where you simply call the function of the corresponding class directly via its pointer.


#include<iostream>
using namespace std;
class Father{
public:  
  void print()
  {
    printf("Father's function!");
  }
};
class Son:public Father
{
public:
  void print()
  {
    printf("Son's function!");
  }
};
int main()
{
  Father f1;
  Son s1;
  Father* f = &f1;
  Son* s = &s1;
  f->print();
  cout<<endl<<endl;
  s->print();
}

2. Base class pointer to derived class object

This is allowed by defining a base class pointer and a derived class object to point to the derived class object, but it is important to note that the pointer usually calls a member function of the base class. There are 4 cases:

1. Functions exist in both base and derived classes

The member function is called with a base class pointer to a derived class object.

[

Father f1;
Son s1;
Father* f = & s1;
f- > print (); The base class member function is called

]

2. Functions do not exist in the base class, but in the derived class

Since the member function in the base class is still being called, an attempt to call a member function that is only available in a derived class through a base class pointer causes the compiler to report an error.

[

error C2039: "xxx" : not a member of "Father"

]

Cast a base class pointer to a derived class pointer

This is a downward cast, after which the base class pointer to the derived class accesses the member functions of the derived class:

[

Son s1;
Father* f = & s1;
Son *s = (Son*)f;
s- > print1 (); // Calls derived class member functions

]

But this cast operation is a potentially dangerous operation.

4. The existence of virtual functions in the base class

If a member function in a base class is defined as a virtual function and is implemented in a derived class, the virtual function is accessed through a base class pointer to a derived class, accessing the implementation in the derived class. Allowing the operation "base class pointer to derived class" is the most significant, through virtual functions and function overrides, to achieve "polymorphism" (point to different derived classes, to achieve different functions).

[

Father f1;
Son s1;
Father* f = & s1;
f- > print (); // Calls derived class member functions

]

A derived class pointer to a base class object

A compilation error is generated. A base class object cannot be treated as a derived class object; a derived class may have members or member functions that are unique to the derived class.

Even if a cast is used to cast a derived class pointer to a base class pointer, the function accessed by this "cast a derived class pointer to a base class" is still a member of the derived class.

[

Father f1;
Son s1;
Son* s= & s1;
Father* f = (Father*) s;
f- > print (); // Calls derived class member functions

]

In summary, derived class methods (casts and virtual functions) can be accessed through base class Pointers, and there is no way to call base class member functions via derived class Pointers (even cast).

conclusion


Related articles: