当前位置:网站首页>子类隐藏父类的同名函数
子类隐藏父类的同名函数
2022-07-03 15:20:00 【宇龍_】
在C++中,子类是不能重载父类函数的,因为重载只发生在同一个类中。
所以在不考虑虚函数重写的情况下,子类中所有的同名函数(仅仅是与父类的函数名称相同),都会导致父类的同名函数被隐藏。
见代码:
#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");
}
在父类CFather和子类CChild中均实现了Test和Print方法,在main函数中声明了一个CChild类的对象,此时调用Test和Print方法,输出结果如下:

以上的输出结果还是很好理解的,CChild类当然调用的是自己实现的方法。
那么以下的代码呢?
#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类中仅仅实现了Print这个同名函数,此时调用CChild从父类继承过来的Test方法,输出结果会怎么样呢?

其实也很好理解,父类的Test方法内部调用的Print方法当然是父类自己的Print方法,因为是普通函数,不具有多态的特性。
根据以上代码,可以总结归纳几点:
1、对于父类而言,子类的所有函数均不可见。
2、对于子类而言,父类中所有的同名函数都被隐藏了。
3、在父类的其他函数中访问同名函数,访问的均为父类的同名函数。
4、在子类的其他函数中访问同名函数,访问的均为子类的同名函数。
内容可能有些重复,但无伤大雅。
PS,顺带提一下,在子类中需要访问父类的同名函数时,一般用__super关键字来访问而不建议用父类的名称,因为会给人一种调用静态方法的感觉,例子如下:
#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(); //建议用__super::
__super::Test();
}
void FatherPrint()
{
//CFather::Print();
__super::Print();
}
};
int main()
{
CChild child;
child.Test();
child.Print();
child.FatherTest();
child.FatherPrint();
system("pause");
}

边栏推荐
- Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
- 秒杀系统1-登录功能
- Redis cache penetration, cache breakdown, cache avalanche solution
- Apache ant extension tutorial
- Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
- 详解指针进阶1
- [pytorch learning notes] transforms
- The markdown file obtains the pictures of the network and stores them locally and modifies the URL
- The state does not change after the assignment of El switch
- 从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
猜你喜欢

Visual upper system design and development (Halcon WinForm) -3 Image control

【pytorch学习笔记】Datasets and Dataloaders

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

Jvm-04-runtime data area heap, method area

qt使用QZxing生成二维码

运维体系的构建

需要知道的字符串函数

Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
![[cloud native training camp] module VIII kubernetes life cycle management and service discovery](/img/87/92638402820b32a15383f19f6f8b91.png)
[cloud native training camp] module VIII kubernetes life cycle management and service discovery

Solve the problem that pushgateway data will be overwritten by multiple push
随机推荐
互斥对象与临界区的区别
socket.io搭建分布式Web推送服务器
Introduction to redis master-slave, sentinel and cluster mode
详解指针进阶2
Halcon and WinForm study section 1
Kubernetes vous emmène du début à la fin
如何使用 @NotNull等注解校验 并全局异常处理
[combinatorial mathematics] binomial theorem and combinatorial identity (binomial theorem | three combinatorial identities | recursive formula 1 | recursive formula 2 | recursive formula 3 Pascal / Ya
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
视觉上位系统设计开发(halcon-winform)
高并发下之redis锁优化实战
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
Chapter 04_ Logical architecture
Summary of JVM knowledge points
在MapReduce中利用MultipleOutputs输出多个文件
Detailed comments on MapReduce instance code on the official website
Markdown file titles are all reduced by one level
Detailed pointer advanced 2
Jvm-06-execution engine
Tensorflow realizes verification code recognition (I)