当前位置:网站首页>Void* C is a carrier for realizing polymorphism
Void* C is a carrier for realizing polymorphism
2022-07-05 08:10:00 【Nanbolwan】
Consider such a question , There is a machine composed of multiple functional blocks , And these plates are freely pluggable , And the parameters of each kind of plate management are also different . We use it C++ This kind of object-oriented language can easily realize such functions , Because object-oriented languages support polymorphism . So there is such a problem :
C How language realizes polymorphism ?
Facing the above practical problems ,C++ This is how it works :
class Board{
public:
Board();
virtual ~Board();
virtual Board* getBoard() = 0;
virtual void setBoard(Board* board)=0;
private:
int boardtype;
Board* board;
}
class Board1 : public Board{
...
}
It is this virtual function that realizes the dynamic management of the board .
C The language itself cannot realize such functions as virtual functions , But we still want to implement similar polymorphic functions ?
Null pointer (void *) Is the only tool we can use , Because a null pointer can point to any data type and can be forcibly converted to any type , Include malloc Such functions also return null pointers , So we use null pointers to solve the above practical problems :
struct Slot
{
int boardtype;
void *board;
};
struct Board1
{
int prarm1;
.
.
.
int prarmn;
}
struct Board2
{
int prarm1;
.
.
.
int prarmn;
}
...
// Card slot insert board
struct Board2 *board = (struct Board2*)malloc(sizeof(struct Board2));
struct Slot slot;
slot.boardtype = 2;
slot.board = (void *)board;
Anyway, this realizes a C Polymorphism of language .
边栏推荐
- UEFI development learning 3 - create UEFI program
- My-basic application 1: introduction to my-basic parser
- Slist of linked list
- Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
- Shell script basic syntax
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- UEFI development learning 6 - creation of protocol
- L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
- Class of color image processing based on Halcon learning_ ndim_ norm. hdev
- Altium designer learning (I)
猜你喜欢

Relationship between line voltage and phase voltage, line current and phase current

L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!

Explain task scheduling based on Cortex-M3 in detail (Part 2)
![[paper reading] the latest transfer ability in deep learning: a survey in 2022](/img/6b/b564fb7a6895329073fb5eaff64340.png)
[paper reading] the latest transfer ability in deep learning: a survey in 2022

Shell脚本基本语法

如何进行导电滑环选型

Altium designer learning (I)

Embedded composition and route

Basic embedded concepts

导电滑环磨损快的原因
随机推荐
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
LED display equipment records of the opening ceremony of the Beijing Winter Olympics
What are the test items of power battery ul2580
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
如何进行导电滑环选型
C WinForm [help interface - send email] - practice five
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
UEFI development learning 5 - simple use of protocol
The firmware of the connected j-link does not support the following memory access
UEFI development learning 2 - running ovmf in QEMU
Hardware and software solution of FPGA key chattering elimination
Ads usage skills
动力电池UL2580测试项目包括哪些
找不到实时聊天软件?给你推荐电商企业都在用的!
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
Record the torch encountered by win10 cuda. is_ False problem in available()
Imx6ull bare metal development learning 1-assembly lit LED
Connection mode - bridge and net
Classic application of MOS transistor circuit design (2) - switch circuit design