当前位置:网站首页>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");
}
边栏推荐
- 软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
- Halcon and WinForm study section 2
- Kubernetes帶你從頭到尾捋一遍
- 视觉上位系统设计开发(halcon-winform)
- Visual upper system design and development (Halcon WinForm) -1 Process node design
- Custom annotation
- MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
- String functions that you need to know
- Calibre LVL
- [daily training] 395 Longest substring with at least k repeated characters
猜你喜欢
Halcon and WinForm study section 1
Seckill system 2 redis solves the problem of distributed session
Summary of JVM knowledge points
详解指针进阶2
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
求字符串函数和长度不受限制的字符串函数的详解
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
What is machine reading comprehension? What are the applications? Finally someone made it clear
Visual upper system design and development (Halcon WinForm) -4 Communication management
Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
随机推荐
Baidu AI Cloud helps Shizuishan upgrade the smart health care model of "Internet + elderly care services"
视觉上位系统设计开发(halcon-winform)-1.流程节点设计
Matplotlib drawing label cannot display Chinese problems
leetcode_ Power of Four
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
Introduction to redis master-slave, sentinel and cluster mode
Popular understanding of linear regression (I)
子类隐藏父类的同名函数
解决pushgateway数据多次推送会覆盖的问题
The state does not change after the assignment of El switch
运维体系的构建
软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
[transformer] Introduction - the original author of Harvard NLP presented the annotated transformer in the form of line by line implementation in early 2018
Calibre LVL
Seckill system 2 redis solves the problem of distributed session
Redis lock Optimization Practice issued by gaobingfa
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
How to use annotations such as @notnull to verify and handle global exceptions
【日常训练】395. 至少有 K 个重复字符的最长子串
Jvm-06-execution engine