VC 類成員函式指標匯出與匯入

2021-09-11 03:29:24 字數 1698 閱讀 6938

vc++ 類成員函式指標匯出與匯入

一、匯入外來函式指標

定義乙個類,類中有幾個函式成員完成相似的功能,根據呼叫者選的模式對相應的函式進行呼叫 在funtion()函式下如果按下面的**呼叫的話,由於這個funtion()頻繁呼叫(成千上萬次).如果每次都進行判斷效能會降低很多.所以準備用類成員函式代替下面的段**.

複製**

switch(iflag)

case 1:

fun1();

break;

case 2:

fun2();

break;

case 3:

fun3();

break;

}

複製**

在類中定義

typedef void (baseclass::*fun)();

然後宣告函式成員

fun m_fun;

在建構函式中根據相應的模式進行指標賦值

複製**

switch(iflag)

複製**

在呼叫出直接用類成員函式指標呼叫.卻報了error c2064: term does not evaluate to a function這個錯誤

funtion()

呵呵…其實原因很簡單…成員函式指標和類成員函式指標的區別就是累成員函式會有乙個this指標需要 加上這個指標上述**改為下面的形式…ok.編譯成功…

funtion()

二、匯出類成員函式指標:

匯出類成員函式指標常用的方式是匯出靜態成員函式指標,但是侷限性表較大,以下整理了非靜態成員函式指標的匯出。

#include 「stdafx.h」

//std::function需要此標頭檔案

#include

#include

#include

//std::find需要此標頭檔案

#include

using namespace std;

/********************************************

first類和second相當於兩個觀察者, 他們兩個沒有繼承結構

first類和second類的更新函式原型相同,函式名不必相同

********************************************/

class first

};class second

typedef std::functionpadd;

class ctest

int _tmain(int argc, _tchar* ar**)

{first objfirst;

second objsecond;

ctest  obj;

padd padd1 = std::bind(&first::add1, &objfirst, std::placeholders::_1, std::placeholders::_2);

padd padd2 = std::bind(&second::add2, &objsecond, std::placeholders::_1, std::placeholders::_2);

obj.run(padd1);

obj.run(padd2);

類成員函式指標

類成員函式指標 類的成員函式分為兩種,一種是靜態函式,另外一種是非靜態函式。例如 class x display 為靜態函式,getvalue即為非靜態函式。兩種函式在使用的時候是不一樣的。靜態函式可以直接由類名來呼叫,而非靜態函式則必須通過某乙個物件來呼叫,例如 x display x x x.g...

類成員函式指標

類成員函式指標的基本用法 1。宣告 已知簡單的類宣告如下 class screen screen public screen forword int num 1 我們可以這樣宣告乙個指向back成員函式的指標 screen screen back fun int para screen back 其...

類成員函式指標

include using namespace std class human 抽象類human class mother public human 派生類mother從抽象類human繼承 覆蓋純虛函式run void eat 覆蓋純虛函式eat class father public human...