当前位置:网站首页>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 [help interface - send email] - practice five
- Problem solving: interpreter error: no file or directory
- Basic information commands and functions of kernel development
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Some thoughts on extracting perspectives from ealfa and Ebeta
- Hardware and software solution of FPGA key chattering elimination
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- [untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
- [trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
- Ads usage skills
猜你喜欢

Talk about the circuit use of TVs tube
![[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc](/img/9e/2524cbb9b90135c54669ba8d5338b7.jpg)
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc

Nb-iot technical summary

C # joint configuration with Halcon

Tailq of linked list

Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)

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

Programming knowledge -- basis of C language

How to copy formatted notepad++ text?

Shell script basic syntax
随机推荐
Ads usage skills
Talk about the function of magnetic beads in circuits
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
Hardware 1 -- relationship between gain and magnification
Soem EtherCAT source code analysis II (list of known configuration information)
Baiwen 7-day smart home learning experience of Internet of things
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
DokuWiki deployment notes
UEFI development learning 4 - getting to know variable services
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Sql Server的存儲過程詳解
Altium designer learning (I)
Network communication model -- Network OSI tcp/ip layering
Tailq of linked list
WiFi wpa_ Detailed description of supplicant hostpad interface
Shell script basic syntax
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication