当前位置:网站首页>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 .
边栏推荐
- Volatile of C language
- Some thoughts on extracting perspectives from ealfa and Ebeta
- Imx6ull bare metal development learning 2- use C language to light LED indicator
- Anonymous structure in C language
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- Problem solving: interpreter error: no file or directory
- Programming knowledge -- basis of C language
- C WinForm [exit application] - practice 3
- Working principle and type selection of common mode inductor
- [trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
猜你喜欢
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Train your dataset with yolov4
Network port usage
Altium designer learning (I)
C WinForm [exit application] - practice 3
Ble encryption details
Communication standard -- communication protocol
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Talk about the circuit use of TVs tube
随机推荐
Stablq of linked list
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
WiFi wpa_ Detailed description of supplicant hostpad interface
Halcon's practice based on shape template matching [1]
Pointnet++ classification practice
Screen record of the opening ceremony of the Beijing winter olympics 2
Connection mode - bridge and net
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
生产中影响滑环质量的因素
Train your dataset with yolov4
Working principle and type selection of common mode inductor
C WinForm [help interface - send email] - practice five
DokuWiki deployment notes
UEFI development learning 2 - running ovmf in QEMU
Hardware and software solution of FPGA key chattering elimination
Gradle composite construction
Classic application of MOS transistor circuit design (2) - switch circuit design
The firmware of the connected j-link does not support the following memory access
Programming knowledge -- assembly knowledge
Consul installation