mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-04-13 17:50:59 +08:00
update
This commit is contained in:
55
basic_content/c_poly/c_examp.c
Normal file
55
basic_content/c_poly/c_examp.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file c_examp.c
|
||||
* @brief C实现多态
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-06
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/// 重定义一个函数指针类型
|
||||
typedef void (*pf) ();
|
||||
|
||||
/**
|
||||
* @brief 父类
|
||||
*/
|
||||
typedef struct _A
|
||||
{
|
||||
pf _f;
|
||||
}A;
|
||||
|
||||
|
||||
/**
|
||||
* @brief 子类
|
||||
*/
|
||||
typedef struct _B
|
||||
{
|
||||
A _b; ///< 在子类中定义一个基类的对象即可实现对父类的继承。
|
||||
}B;
|
||||
|
||||
void FunA()
|
||||
{
|
||||
printf("%s\n","Base A::fun()");
|
||||
}
|
||||
|
||||
void FunB()
|
||||
{
|
||||
printf("%s\n","Derived B::fun()");
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
A a;
|
||||
B b;
|
||||
|
||||
a._f = FunA;
|
||||
b._b._f = FunB;
|
||||
|
||||
A *pa = &a;
|
||||
pa->_f();
|
||||
pa = (A *)&b; /// 让父类指针指向子类的对象,由于类型不匹配所以要进行强转
|
||||
pa->_f();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user