C + + Learning Journey-Combat 3-using function functions in struct structs

Source: Internet
Author: User

Coding section:

#include <stdio.h>structstudent{intID; Char*Name; intAge ; void(*tostring) (intIdChar* Name,intAge );};//defines the structure body that contains student ID, name, age, and output function pointers studentvoidToString (intIdChar* Name,intAge ) {printf ("ID value of the mystudent instance variable:%d\n", id); printf ("the name value of the mystudent instance variable:%s\n", name); printf ("Age value of mystudent instance variable:%d\n", age); }//output function, you need to link an internal function pointer to the function when instantiating the struct.intMain () {structStudent mystudent = {1,"Luo Shuai", -}; Mystudent.tostring= toString;//link an external tostring function to a function pointer inside a structMystudent.tostring (Mystudent.id,mystudent.name,mystudent.age);//calling struct intrinsic functions    return 0;//Exit Program}

Attached (and the effect of the previous essay is the same, just encapsulated into the structure of the inner function):

The author reminds: Because in C, the structure inside does not allow the definition of function, so can only be defined by the external function, and in the structure of the function pointer, and then through the pointer to link to the function, so as to achieve the effect of invoking the structure of the inner function.

In the next article, we will start to try to use what we have learned, to do a basic calculator function, come on!

C + + Learning Journey-Combat 3-using function functions in struct structs

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.