模板作為引數

2021-10-06 11:03:38 字數 1829 閱讀 9798

模板這種也是可以作為引數的

template ,其中template class 就是帶乙個泛型的型別,thing是引數,意味著,thing接受帶乙個泛型引數的型別.

利用乙個類模板作為作為標頭檔案:

template

<

typename type>

class

stack

;// constant specific to class

type items[max]

;// holds stack items

int top;

// index for top stack item

public

:stack()

;bool

isempty()

;bool

isfull()

;bool

push

(const type & item)

;// add item to stack

bool

pop(type & item)

;// pop top into item};

template

<

class

type

>

stack

::stack()

template

<

class

type

>

bool stack

::isempty()

template

<

class

type

>

bool stack

::isfull()

template

<

class

type

>

bool stack

::push

(const type & item)

else

return

false;}

template

<

class

type

>

bool stack

::pop

(type & item)

else

return

false

;}

乙個簡單的棧模板類

下面是是乙個接受帶乙個引數的模板的模板類

template

<

template

<

typename t>

class

thing

>

class

crab

bool

push

(int a ,

double x)

bool

pop(

int& a ,

double

& x)

};

測試函式如下:

int

main()

while

(nebual.

pop(ni , nb)

)// 都不空列印

cout << ni <<

", "

<< nb << endl;

cout <<

"done.\n"

;return0;

}

結果如下:

指標作為引數

值傳遞,指標傳遞?這幾天在學習c過程中,在使用指標作為函式引數傳遞的時候出現了問題,根本不知道從何得解 源 如下 createnode binnode tree,char p 該 段的意圖是通過乙個函式建立乙個二叉樹的節點,然而在,呼叫該函式後,試圖訪問該節點結構體的成員時候,卻發生了記憶體訪問錯誤...

物件作為引數

在這個例子中,我們將isequal 功能是價值流的乙個朋友。isequal 取兩個值物件作為引數。因為isequal 是價值類的朋友,它可以訪問所有的值物件的私有成員。在這種情況下,它使用的訪問在兩個物件做乙個比較,並返回true,如果他們是平等的。乙個函式可以同時對多個類的乙個朋友。例如,考慮下面...

c 變長引數的作為引數

有時候不僅需要處理變長引數,還需要將變長引數作為引數,傳遞給其他函式。函式說明write log,向檔案中寫字串 param pf 檔案指標 param format 格式字串 與printf格式相同 param 可變長引數 void write log file pf,const char for...