Object-Oriented Programming in C Language

Source: Internet
Author: User

The following is an introduction to the implementation of the struct function in C. Only function pointer members can be used. The C structure body does not have function code, but can have function pointers. C/C codeCode highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/

#include <stdio.h>    struct test  {      void fun()      {          printf("hello,world\n");      }  };    int main()  {      struct test _t;      _t.fun();      return 0;  }  

 

The above code is saved as. c, which is not available in VC 6.0 and Dev Cpp. Function pointer implementation, rather than directly defining functions... Of course, struct can put function pointers. For example: C/C codeCode highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
#include <stdio.h>  void fun()  {      printf("hello,world\n");  }     struct test  {      void (*Fun)();  };     int main()  {      struct test _t;      _t.Fun = fun;         (*_t.Fun)();      return 0;  }     

 

The C structure body does not have function code, but it can have function pointers. The netizen replied: struct in pure C does not have a member function, but it can have function pointers. Object-oriented programming with ANSI-C is used to simulate member functions with function pointers. For more information about how to use the C language in Linux source code, see the Linux kernel source code.
#include<stdio.h>    struct MyClass  {          char* name;             int age;          void (*funnull) ();          void (*func) (struct MyClass mc);  };    void realfunnull()  {          printf("hello world!\n");  }    void realfunc(struct MyClass mc)  {          printf("MyClass's name is:%s\n",mc.name);          printf("MyClass's age is:%d\n",mc.age);  }    int main()  {          struct MyClass mc = {"Simon", 25, realfunnull, realfunc};          mc.funnull();          mc.func(mc);          return 0;  }  

 

Related Article

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.