In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the implementation process of C++ polymorphism, in response to this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.
Polymorphism describes the ability to manipulate multiple types using pointers or references to base classes.
We know that pointers to subclasses can be implicitly converted to parent classes, so if we want to process an unknown type when programming, we can declare the type of the parameter as a pointer to the parent class when declaring the method.
This means that we implement different methods depending on the type passed in. The key here is that when a subclass overrides a virtual method of its parent class, it overrides the virtual method of the parent class at the corresponding position in the virtual method table.
For example:
File classFile.h:
#ifndef classFile_Header_File #define classFile_Header_File class father { public: virtual void show(); }; class son: public father { public: void show(); void sayhi(); }; #endif
Here we implement override on the parent class in the subclass (C++ doesn't provide the override keyword, so be careful about overriding the methods of the parent class).
TestPoly.cpp code is as follows:
#include #include"classFile.h" using namespace std; void hello(father* fp) { fp->show(); } int main() { father f; hello(&f); son s; hello(&f); } inline void father::show() { cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.