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