Understanding of pure c struct and c ++ struct, and understanding of c struct

Source: Internet
Author: User

Understanding of pure c struct and c ++ struct, and understanding of c struct

// Main. cpp

// If the file name is. the end of cpp indicates that this is a c ++ source program. // in the c ++ source program, the role of class is exactly the same as that of struct, // except for their default member attributes (the class is private by default, // struct is common by default), and struct has this pointer, and struct // can inherit, struct also supports all the attributes of c ++ polymorphism. Also, struct // has a virtual table. Below is an example of a struct virtual table. // Remember that in the source file at the End of. cpp, struct is basically the same as the class. # Include <stdlib. h> # include <stdio. h> struct A {virtual void fun () = 0 ;}; struct B: public A {void fun () {printf ("B: fun () \ n ") ;}}; typedef void (* PF) (); void Printf (void * arg) {PF f = PF (* (int *) arg ); f () ;}void test () {B B; Printf (int *) * (int *) & B) ;}int main () {test (); return 0 ;}


// Main. c

# Include <stdlib. h> # include <stdio. h> // because the struct attributes in pure c language are different from those in c ++. // First, struct in pure C language does not support polymorphism and cannot be constructed. // without this, there cannot be a function, if we use a pure c struct to simulate a class, it will be very difficult. below is my rough writing. Struct A; typedef void (* PF) (struct A * this); // array of function pointers. Struct vPtrNode; typedef void (* PV) (struct vPtrNode * this); struct vPtrNode {PV pf; // simulate a virtual table .}; Void vtrprintf (struct vPtrNode * this) {printf ("vtrprintf () \ n");} struct A {PF f; // because the struct in pure C language cannot define functions, function pointers are used instead. Int a; struct vPtrNode * vptr; struct B {PF p; struct A * B; // simulate inheritance so that struct B calls A function in Base Class.} B ;}; void fun1 (struct A * this) // simulate the this pointer. {This-> a = 100; printf ("fun1 (): a = % d \ n", this-> a);} int main () {struct A; a. f = (PF) fun1; // simulate this. A. f (& a); //. b. p = (PF) fun1; // The pattern subclass function p overwrites function f in the base class. A. b. p (& a);. b. B = (struct A *) malloc (sizeof (struct A); // space must be opened here. If no space is opened, A block error occurs when you assign a definite // function address to the pointer. A. B. B-> f = (PF) fun1; a. B. B-> f (& a); // simulate a subclass to call the function f of the parent class. /// // The virtual table only exists in the parent class. A. vptr = (struct vPtrNode *) malloc (sizeof (struct vPtrNode);. vptr-> pf = (PV) vtrprintf; struct vPtrNode vptr;. vptr-> pf (& vptr); return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.