Precautions when the parent class is a formal parameter and the child class is an actual parameter

Precautions when the parent class is a formal parameter and the child class is an actual parameter

Direct value pass

Code example

#include <iostream>  
using namespace std;  
  
class Fish  
{  
public:  
    virtual void ShowInf()  
    {  
        cout << "我是一条fish" << endl;  
    }  
};  
  
class Carp : public Fish  
{  
public:  
    void ShowInf()  
    {  
        cout << "我是一条carp" << endl;  
    }  
};  
  
void ShowInf(Fish fish)  // 直接进行普通的值传递
{  
    fish.ShowInf();  
}  
  
int main()  
{  
    Carp carp;  
    ShowInf(carp);  
}  

 

What does the code tell?

When the function parameter is the parent class object and the passed-in parameter is the subclass object, but we use ordinary value transfer, the subclass object only copies the part of the data it inherits to the subclass formal parameter.

operation result

 

Pass by reference

Code example

#include <iostream>  
using namespace std;  
  
class Fish  
{  
public:  
    virtual void ShowInf()  
    {  
        cout << "我是一条fish" << endl;  
    }  
};  
  
class Carp : public Fish  
{  
public:  
    void ShowInf()  
    {  
        cout << "我是一条carp" << endl;  
    }  
};  
  
void ShowInf(Fish& fish)  // 进行引用值传递
{  
    fish.ShowInf();  
}  
  
int main()  
{  
    Carp carp;  
    ShowInf(carp);  
}  

 

What does the code tell?

 

When using & reference to pass by value, it is quite castrated as a parent object, that is, the part that only belongs to itself is cut off. Then pass the remaining part to the function as actual parameters.

What we should pay attention to here is the virtual function. When the parent class has a virtual function with the same name as the child class, the member function of the child class will override the virtual member function in the parent class. Therefore, the remaining part of castration is not the same as the original parent member. Refer to "Virtual Function Table" for the function principle of virtual function.

Virtual function table

We see that the ShowInf() function in the parent class is declared as a virtual type. How does the virtual function work?

My personal understanding: the virtual word of the virtual function can be understood as "it is the same as a virtual function". When we declare the ShowInf() function of the parent class as a virtual function and we also declare a ShowInf() function in the subclass, then in the subclass, ShowInf( ) The function can just overwrite the function of the same name in the parent class (function of the same name).

For detailed explanation, please refer to: https://blog.csdn.net/weixin_45590473/article/details/107296026

operation result

 

Use deep copy for delivery

#include <iostream>  
using namespace std;  
  
class Fish  
{  
public:  
    virtual void ShowInf()  
    {  
        cout << "我是一条fish" << endl;  
    }  
};  
  
class Carp : public Fish  
{  
public:  
    void ShowInf()  
    {  
        cout << "我是一条carp" << endl;  
    }  
};  
  
void ShowInf(Fish* fish)  
{  
    dynamic_cast<Carp*>(fish)->ShowInf(); // 进行了有继承关系类之间的强制类型转换  
}  
  
int main()  
{  
    Carp carp;  
    ShowInf(&carp);  
}  

 

Deep copy actually transfers and manipulates the pointer of the object, because when we get the address (pointer content), we can adjust the pointer type to access different valid ranges. The valid range only refers to the same area in the memory as the pointer type. , Does not castrate part of the content like the way of value transfer, using pointers as formal parameters is both flexible (the pointer type can be adjusted to adjust the pointer to the content) and efficient (the pointer transfer is much faster than the value transfer).

Guess you like

Origin blog.csdn.net/weixin_45590473/article/details/108328192