当前位置:网站首页>Subclass hides the function with the same name of the parent class
Subclass hides the function with the same name of the parent class
2022-07-03 15:26:00 【Yulong_】
stay C++ in , A subclass cannot overload a parent function , Because overloading only occurs in the same class .
So without considering the rewriting of virtual functions , All functions with the same name in the subclass ( Just the same as the function name of the parent class ), Will result in The function with the same name of the parent class is hidden .
See the code :
#include <stdio.h>
#include <stdlib.h>
class CFather
{
public:
void Test()
{
Print();
}
void Print()
{
printf("CFather\n");
}
};
class CChild:public CFather
{
public:
void Test()
{
Print();
}
void Print()
{
printf("CChild\n");
}
};
int main()
{
CChild child;
child.Test();
child.Print();
system("pause");
}
In the parent class CFather And subclasses CChild Both of them have realized Test and Print Method , stay main A... Is declared in the function CChild Class object , Call at this time Test and Print Method , The output is as follows :

The above output results are well understood ,CChild Of course, a class calls its own implemented methods .
What about the following code ?
#include <stdio.h>
#include <stdlib.h>
class CFather
{
public:
void Test()
{
Print();
}
void Print()
{
printf("CFather\n");
}
};
class CChild:public CFather
{
public:
void Print()
{
printf("CChild\n");
}
};
int main()
{
CChild child;
child.Test();
child.Print();
system("pause");
}CChild Class only implements Print This function with the same name , Call at this time CChild Inherited from the parent class Test Method , What happens to the output ?

It's easy to understand , Of the parent class Test Method called internally Print The method is of course the parent class's own Print Method , Because it's an ordinary function , Not characterized by polymorphism .
According to the above code , We can sum up a few points :
1、 For the parent class , All functions of subclasses are invisible .
2、 For subclasses , All functions with the same name in the parent class are hidden .
3、 Access the function with the same name in other functions of the parent class , All functions accessed are functions with the same name of the parent class .
4、 Access the function with the same name in other functions of the subclass , All accessed functions with the same name are subclasses .
There may be some repetition , But it doesn't hurt .
PS, by the way , When the subclass needs to access the function with the same name of the parent class , It's usually used __super keyword To visit and It is not recommended to use the name of the parent class , Because it gives people the feeling of calling static methods , Examples are as follows :
#include <stdio.h>
#include <stdlib.h>
class CFather
{
public:
void Test()
{
Print();
}
void Print()
{
printf("CFather\n");
}
};
class CChild:public CFather
{
public:
void Test()
{
Print();
}
void Print()
{
printf("CChild\n");
}
void FatherTest()
{
//CFather::Test(); // Suggest using __super::
__super::Test();
}
void FatherPrint()
{
//CFather::Print();
__super::Print();
}
};
int main()
{
CChild child;
child.Test();
child.Print();
child.FatherTest();
child.FatherPrint();
system("pause");
}

边栏推荐
- Popular understanding of ovo and ovr
- Tensorflow realizes verification code recognition (I)
- Atlas atlas torque gun USB communication tutorial based on mtcom
- Visual upper system design and development (Halcon WinForm) -3 Image control
- [attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
- Matlab r2011b neural network toolbox precautions
- Apache ant extension tutorial
- 【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
- 如何使用 @NotNull等注解校验 并全局异常处理
- Detailed comments on MapReduce instance code on the official website
猜你喜欢

VS2017通过IP调试驱动(双机调试)

子类隐藏父类的同名函数

Halcon与Winform学习第一节

Jvm-09 byte code introduction

Final review points of human-computer interaction

GCC cannot find the library file after specifying the link library path

Detailed explanation of string function and string function with unlimited length

Kubernetes带你从头到尾捋一遍

Redis主从、哨兵、集群模式介绍

基础SQL教程
随机推荐
Solve the problem that pushgateway data will be overwritten by multiple push
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
秒杀系统3-商品列表和商品详情
Stress test WebService with JMeter
What is embedding (encoding an object into a low dimensional dense vector), NN in pytorch Principle and application of embedding
驱动与应用程序通信
[set theory] inclusion exclusion principle (complex example)
Visual upper system design and development (Halcon WinForm) -5 camera
VS2017通过IP调试驱动(双机调试)
WinDbg分析dump文件
视觉上位系统设计开发(halcon-winform)-5.相机
Popular understanding of ovo and ovr
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
解决pushgateway数据多次推送会覆盖的问题
Kubernetes帶你從頭到尾捋一遍
高并发下之redis锁优化实战
Tensorflow realizes verification code recognition (II)