其他分享
首页 > 其他分享> > OO With Function Pointers

OO With Function Pointers

作者:互联网

Hareesh Nagarajan (hnagaraj AT cs uic edu)
Mon Nov 21 00:58:19 CST 2005

This document may never be updated

#include <stdio.h>
struct X {
	int x;
	void (*fp)(struct X *);
};

void func1(struct X *xobj) { printf("%s, %d\n", __FUNCTION__, xobj->x); }
void func2(struct X *xobj) { printf("%s, %d\n", __FUNCTION__, xobj->x); }

main() {
	struct X obj;
	obj.x  = 10;
	obj.fp = func1;
	obj.fp(&obj);
	obj.x  = 20;
	obj.fp = func2;
	obj.fp(&obj);
}

Output

$ ./a.out
func1, 10
func2, 20

Cool eh?

 

copyright

https://www2.cs.uic.edu/~hnagaraj/articles/function-pointers/

 

标签:OO,Function,func1,obj,struct,Pointers,xobj,fp,__
来源: https://www.cnblogs.com/dong1/p/14976967.html