当前位置:网站首页>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 .
边栏推荐
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- Process communication mode between different hosts -- socket
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- UEFI development learning 5 - simple use of protocol
- Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
- Nb-iot technical summary
- Programming knowledge -- assembly knowledge
- Beijing Winter Olympics opening ceremony display equipment record 3
- How to define guid in AMI code
- Verilog -- state machine coding method
猜你喜欢
Network communication model -- Network OSI tcp/ip layering
C WinForm [display real-time time in the status bar] - practical exercise 1
Consul installation
H264 (I) i/p/b frame gop/idr/ and other parameters
Semiconductor devices (III) FET
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Nb-iot technical summary
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
C # joint configuration with Halcon
Compilation warning solution sorting in Quartus II
随机推荐
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Beijing Winter Olympics opening ceremony display equipment record 3
Several important parameters of LDO circuit design and type selection
Gradle composite construction
Gradle复合构建
导电滑环磨损快的原因
Talk about the circuit use of TVs tube
Random function usage notes
Hardware 3 -- function of voltage follower
OLED 0.96 inch test
C WinForm [realize the previous and next selection pictures] - practice 7
IEEE access personal contribution experience record
How to migrate the device data accessed by the RTSP of the easycvr platform to easynvr?
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Design a clock frequency division circuit that can be switched arbitrarily
Vofa+ software usage record
Screen record of the opening ceremony of the Beijing winter olympics 2
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
VESC Benjamin test motor parameters