C 筆記( 模板)

2021-10-01 12:39:11 字數 4702 閱讀 9494

友元函式和友元類

//例子

class

coordinate

;//對於上述全域性友元函式的使用方法

//1.直接訪問私有成員

void

printxy

(coordinate &c)

//2.傳入物件名而非位址

intmain

(void

)

//例子,將乙個類的成員函式宣告為另外乙個類的友元

class

coordinate

;//對於友元成員函式的使用

class

circle};

intmain

(void

)

友元函式的風險在於破壞了資料的封裝型,在友元函式中改變了資料的值,也不易察覺。

除非有特殊需要,並不建議大家處處使用友元。

class

circle

;class

coordinate

;//circle這個類可以任意訪問coordinate中的資料

class

circle

private

: coordinate m_coor;

};

關於友元的注意事項:

關鍵字:static

靜態資料成員必須單獨初始化

靜態成員函式不能呼叫非靜態成員函式和非靜態資料成員

靜態資料成員只有乙份,且不依賴物件而存在(例如,用sizeof()函式去判斷乙個物件的大小,結果不包括該類的資料成員)

const和static不能連在一起用

//例子

class

tank

~tank()

static

intgetcount()

static

int s_icount;

private

: string m_strcode;};

int tank::s_icount =0;

//訪問方法有兩種,不通過物件直接通過類來訪問,或者是通過物件來訪問

intmain

(void

)

運算子過載

概念:給原有的運算子賦予新的功能

例如:使用+號來做運算子的拼接

本質:函式過載

關鍵字:operator

一元運算子過載

通過友元函式過載:

//友元函式的過載

class

coordinate

;//實現時

coordinate&

operator

-(coordinate &coor)

//使用時

intmain

(void

)

成員函式過載:

//成員函式的過載

class

coordinate

;//實現時

coordinate& coordinate::

operator-(

)//使用時

intmain

(void

)

前置++符號過載

class

coordinate

;//實現時

coordinate& coordinate::

operator++(

)//使用時

intmain

(void

)

後置++符號過載

class

coordinate

;//實現時

coordinate coordinate::

operator++(

int)

//使用時

intmain

(void

)

二元運算子過載

友元函式過載

class

coordinate

;//實現時

coordinate operator+(

const coordinate &c1,

const coordinate &c2)

//使用時

intmain

(void

)

成員函式過載

class

coordinate

;//實現時

coordinate coordinate::

operator+(

const coordinate &coor)

//使用時

intmain

(void

)

class

coordinate

;//實現該函式時

ostream&

operator

<<

(ostream &out,

const coordinate &coor)

//使用時

intmain

(void

)

class

coordinate

;//實現該函式時

int coordinate::

operator

(int index)if(

1==index);}

//使用時

intmain

(void

)

模板函式與模板類

函式模板

//函式1

intmax

(int a,

int b)

//函式2

float

max(

float a,

float b)

//函式3

char

max(

char a,

char b)

最好有一種方案,把型別作為引數傳進去,而通過計算機把這三個函式做出來

關鍵字:template typename class

template

<

class

t>

t max

(t a,t b)

//函式模板

//使用時

int ival=

max(

100,99)

;//模板函式

//通過typename定義函式模板

template

<

typename t>

void

swap

(t& a,t& b)

變數作為模板的引數

template

<

int size>

void

display()

//使用時

display<

10>()

;

多引數函式模板

template

<

typename t,

typename c>

void

display

(t a,c b)

typename和class可以混用

函式模板與過載:通過函式模板,可以得到不同的模板函式,這些模板函式之間就形成了過載

類模板

//先舉個例子

template

<

class

t>

class

myarray

private

: t *m_parr;};

//等到定義這個成員函式的時候

template

<

class

t>

void myarray

::display()

//使用時

intmain

(void

)

模板**不能分離編譯,必須全部放在同乙個.**件中

標準模板庫(stl: standard template lib)

vector向量

本質:對陣列的封裝

特點:讀取能在常數時間完成

//初始化vector物件的方式

//vector儲存型別為t的物件。預設建構函式v1為空

vector v1;

//v2是v1的乙個副本

vector

v2(v1)

;//v3包含n個值為i的元素

vector

v3(n,i)

;//v4包含有值初始化元素的n個副本

vector

v4(n)

;

迭代器:

int

main

(void

)return0;

}

鍊錶list

特點:資料插入速度快

map:對映

鍵值對

C 模板筆記

模板 實現 重用機制 圖1 模板 模板類 模板函式和物件之間的關係 一 函式模板 建立通用函式,函式返回型別和形參不具體指定,用模板實參例項化的函式稱為模板函式。template t為型別引數 t max t x,t y 非函式模板過載 函式模板過載 二 類模板 template 類模板名 實際型別...

c 筆記 模板

template 函式名 模板實參表 在函式模板中允許使用多個型別引數,但是,應當注意template定義部分的每個型別引數前必須有關鍵字typename或class 在template語句與函式模板定義語句之間不允許插入別的語句 同一般函式一樣,函式模板也可以過載 函式模板與同名的非模板函式可以過...

C 模板(格式)筆記

參考 c 模板使用介紹 c stl 學習 for each 與仿函式 學習一項技術,就先要熟悉基本語法格式,對於c 我是半路出家。它和c不同的那些關鍵字,真的讓人很頭暈。1.explicit constructor 是對於建構函式的修飾符,說明只支援顯式轉換 關於顯式和隱式轉換 隱式轉換有時會造成不...