C++函式指標的運用及使用JAVA的替代方法

最近同事幫新進同事講解函式指標的妙用,在這裡做一下心得及替代方法比較

C & C++ 函式指標
語法:    
type (*functionname)([type],..)    ->  回傳型態 (*函式名稱)(參數,..)

例:
#include "stdafx.h"
#include 
using namespace std;
void printA(){
     cout << "printA" << endl;
}
void printB(){
     cout << "printB" << endl;
}
       
void printC(){
     cout << "printB" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
        void (*printValue[3])();        
         printValue[0] = printA;
         printValue[1] = printB;
         printValue[2] = printC;
        for( int i=0 ; i<3 3="" i="" span="">
           printValue[i]();
        } 
        system("PAUSE");
        return 0;
}
RUN....
printA
printB
printC
或許你會覺得void (*printValue[3])()宣告方式不好看,你也可以這樣做
       typedef void (*printValue)();     //將其宣告為指標資料型態
       printValue pv[3];      //也可以用  printValue *pv = new printValue[3];
       pv[0] = printA;
       pv[1] = printB;
       pv[2] = printC;
       for( int i=0 ; i<3 i="" span="">
             pv[i]();
       } 
        //delete [] pv;     //雖然這裡可以不用,但使用new 時請習慣加上delete
由上例知道函式指標可以只寫一行程式,經由陣列index值就可執行不同程式功能。 接下來我們將使用C++物件的方式達到相同的功能 C++ 物件的作法來達成上述函式指標達成的功能
#include "stdafx.h"
#include 

using namespace std;

class Base {
    public:
        virtual void printValue() = 0;   //注意!要宣告純虛擬函式,這樣編譯器才會使用晚期繫結(late binding)
};

class PrintA : public Base {
    public:
    void printValue(){
        cout << "printA" << endl;
    }
};

class PrintB : public Base {
    public:
    void printValue(){
        cout << "printB" << endl;
    }
};

class PrintC : public Base {
    public:
    void printValue(){
        cout << "printC" << endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
        Base *pv2[3]; 
        pv2[0] = new PrintA;
        pv2[1] = new PrintB;
        pv2[2] = new PrintC;
        
        for( int i=0 ; i<3 i="" span="">
             pv2[i]->printValue(); 
            delete pv2[i];                     //用完,歸還記憶體
        } 
        system("PAUSE");
        return 0;
}

RUN....

printA
printB
printC
ok 一樣的功能,以物件方式來達成 Java 的作法 java沒有所謂的早期繫結,都是動態繫結(Dynamic Binfing)也就是晚期繫結 java的實作方式是藉由抽象類別(abstract class)來達成,如下例
abstract class Base {
    public abstract void printValue();
}

class printA extends Base {
    @Override
    public void printValue(){
            System.out.println("printA");
        }
}

class printB extends Base {
    @Override
    public void printValue(){
            System.out.println("printB");
        }
}

class printC extends Base {
    @Override
    public void printValue(){
            System.out.println("printC");
        }
}

public class TestJavaFnPT {

    public static void main(String[] argv){
            Base[] pv3 = new Base[2];
            pv3 [0] = new printA();
            pv3 [1] = new printB();
            pv3 [2] = new printC();
            for( int i=0 ; i<3 i="" span="">
                pv3 [i].printValue();
            }
    }
}       

RUN....

printA
printB
printC
OK  我們所要的目的是不是都能達成呢?相對於函式指標、以物件的方式來表達是不是更容易理解呢?

留言

這個網誌中的熱門文章

C# 調用 C/C++ DLL 資料型態之對應

在Ubuntu上安裝EZ100PU晶片讀卡機