struct本身不能支持動態綁定,為了實現統一接口,我們希望實現動態綁定。
例如在不同的操作系統下,對文件的操作是不同,但是我們希望統一結構,有如下結構
struct OSFile
{
PIOMethod pMethods;
/*
其他信息
*/
};
struct WinOSFile
{
PIOMethod pMethods;
..........
};
struct UnixOSFile
{
PIOMethod pMethods;
..........
};
有如下方法:
void UniAPI(OSFile *file);
為了實現動態綁定,做法是在調用該方法之前,將WinOSFile或者UnixOSFile的pMethods賦給OSFile的pMethods從而實現動態綁定。