网站首页 > 精选文章 / 正文
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来“深入C++(二十一)——多态”,欢迎您的访问。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear, this is the LearingYard Academy!
Today, Dive into C++ (21) - Polymorphism ,welcome to visit!
一、基本语法
1. Basic grammar
编写一个执行各种动物叫声的全局函数doSpeak。一种动物就是一个类,每种动物有着不同的叫声,都能通过这个全局函数执行。
Write a global function called doSpeak that executes various animal calls. An animal is a class, each with a different call, that can be executed by this global function.
但执行后可以发现,即使创建的是猫类对象,但并没有输出猫的叫声。为什么?因为全局函数的参数是父类引用,即使可以指向子类对象,但仍只能调用父类中的函数,函数的地址是不变的。
But if you execute it, you can see that even though you created a cat object, it doesn't output a cat meow. Why? Because the arguments of global functions are superclass references, even though they can point to child objects, only functions in the superclass can be invoked, and the addresses of functions remain the same.
那该如何实现子类函数的调用呢?将父类的函数地址设为可变的,即虚函数的定义。这样,子类重写父类的虚函数时,子类的函数会被记录到父类的虚函数表中。子类对象就可以通过父类指针或引用调用到自己的函数。
How do you implement subclass function calls? We set the address of the superclass's function to mutable, which is the definition of the virtual function. This way, when a child class overrides a superclass's virtual function, the child class's function is recorded in the superclass's vtable. Subclass objects can then be called into their own functions by means of a superclass pointer or reference.
如此一来,每个动物都能调用到自己的叫声函数,实现多态。
In this way, each animal can call its own call function, achieving polymorphism.
二、 纯虚函数和抽象类
2. Pure virtual functions and abstract classes
上述案例中,父类的虚函数是没有任何意义的。为了简洁,一般将父类的虚函数改成纯虚函数。
In this case, the virtual function of the superclass has no meaning. For the sake of brevity, it is common to change the virtual function of the superclass to a pure virtual function.
而含有纯虚函数的类被称为抽象类。抽象类不允许实例化对象。
A class that contains pure virtual functions is called an abstract class. Abstract classes are not allowed to instantiate objects.
抽象类的子类需要重写父类中的虚函数,否则不能实例化对象。
A subclass of an abstract class needs to override a virtual function in its superclass or it cannot instantiate an object.
三、 虚析构和纯虚析构
3. Virtual destruction and pure virtual destruction
当需要释放子类对象时,父类指针无法调用子类的析构,导致部分内存没有释放,造成内存泄漏。
When the child class object needs to be deallocated, the parent class pointer cannot call the destruction of the child class, resulting in some memory not being deallocated, causing a memory leak.
通过将父类析构替换成虚析构,利用虚函数的特性,使得子类析构也能调用,避免内存泄漏。
By replacing the parent destructor with virtual destructor, the property of virtual function is used to make the child destructor can also be called to avoid memory leak.
纯虚析构与纯虚函数的格式一样。但若想要知道析构是否被调用,可以在函数外进行编写。
Pure virtual destructors have the same format as pure virtual functions. But if you want to know if the destructor has been called, you can write it outside the function.
今天的分享就到这里了,
如果您对文章有独特的想法,
欢迎给我们留言。
让我们相约明天,
祝您今天过得开心快乐!
That's all for today's sharing.
If you have a unique idea about the article,
please leave us a message,
and let us meet tomorrow.
I wish you a nice day!
翻译:网易有道翻译
本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.
文案|Dongyang
排版|Dongyang
审核|hong
Learning Yard 新学苑
Tags:abstract class
猜你喜欢
- 2024-12-25 1.行为模式-责任链模式 责任链模式实例
- 2024-12-25 深度解析设计模式七大原则之——开闭原则
- 2024-12-25 31 道设计模式面试题,助你在技术面试中取得成功!
- 2024-12-25 一文学会设计模式,太详细了 文学设计是什么
- 2024-12-25 阿里面试:什么是责任链模式? 责任链模式uml图
- 2024-12-25 5.行为模式-中介者模式 中介的形象
- 2024-12-25 一篇文章搞懂C#的五大设计原则 c#设计模式原则
- 2024-12-25 业务数据治理体系化思考与实践 业务数据管理制度
- 2024-12-25 接口和抽象类的区别 接口和抽象类的区别和联系
- 2024-12-25 C# abstract和virtual关键字的区别