当前位置:网站首页>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");
}

边栏推荐
- [transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
- Atlas atlas torque gun USB communication tutorial based on mtcom
- Visual upper system design and development (Halcon WinForm) -1 Process node design
- 视觉上位系统设计开发(halcon-winform)-4.通信管理
- 视觉上位系统设计开发(halcon-winform)-3.图像控件
- mysql innodb 存储引擎的特性—行锁剖析
- 需要知道的字符串函数
- 使用Tengine解决负载均衡的Session问题
- PyTorch crop images differentiablly
- Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
猜你喜欢

Visual host system design and development (Halcon WinForm)

Halcon与Winform学习第一节

What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry

Creation and destruction of function stack frames

Functional modules and application scenarios covered by the productization of user portraits

Tensorflow realizes verification code recognition (III)

北京共有产权房出租新规实施的租赁案例

Digital image processing -- popular understanding of corrosion and expansion

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

QT use qzxing to generate QR code
随机推荐
redis单线程问题强制梳理门外汉扫盲
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
Detailed explanation of string function and string function with unlimited length
What is one hot encoding? In pytoch, there are two ways to turn label into one hot coding
Jvm-04-runtime data area heap, method area
The method of parameter estimation of user-defined function in MATLAB
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
App global exception capture
[set theory] inclusion exclusion principle (complex example)
Markdown file titles are all reduced by one level
Calibre LVL
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
解决pushgateway数据多次推送会覆盖的问题
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
Halcon and WinForm study section 2
[probably the most complete in Chinese] pushgateway entry notes
Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
基于SVN分支开发模式流程浅析
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team