当前位置:网站首页>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 .
边栏推荐
- Train your dataset with yolov4
- Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
- Naming rules for FreeRTOS
- Gradle composite construction
- Communication standard -- communication protocol
- Hardware and software solution of FPGA key chattering elimination
- H264 (I) i/p/b frame gop/idr/ and other parameters
- [tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
- Classic application of MOS transistor circuit design (2) - switch circuit design
- Tailq of linked list
猜你喜欢
Some thoughts on extracting perspectives from ealfa and Ebeta
Hardware and software solution of FPGA key chattering elimination
Process communication mode between different hosts -- socket
C language enhancement -- pointer
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
Solutions to compilation warnings in Quartus II
STM32 tutorial triple ADC interleaved sampling
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Ble encryption details
随机推荐
Halcon's practice based on shape template matching [1]
Halcon's practice based on shape template matching [2]
C WinForm [view status bar -- statusstrip] - Practice 2
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Sql Server的存儲過程詳解
C WinForm [change the position of the form after running] - Practical Exercise 4
动力电池UL2580测试项目包括哪些
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
C language enhancement -- pointer
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Semiconductor devices (III) FET
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Programming knowledge -- assembly knowledge
Several important parameters of LDO circuit design and type selection
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
C WinForm [exit application] - practice 3
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
DokuWiki deployment notes
Vofa+ software usage record