c 學習 基礎 過載函式 實參型別轉換

2021-10-21 03:54:40 字數 1084 閱讀 9218

/*

date: 05/03/21 16:36

description: 過載函式:實參型別轉換

轉換等級

1.精確匹配

2.通過型別提公升

3.通過標準轉換

4.通過類型別轉換

引數匹配和列舉型別

過載和const形參

*/#include

using

namespace std;

class

account

account

(int x)

:num

(x)public

:int num;};

enum tokens

;void

lookup

(account &x)

void

lookup

(const account &x)

voidf(

int*p)

//void f(int *const p) //指標是const,指向的物件不是const [error] redefinition of 'void f(int*)'

//voidf(

const

int*p)

//指標不是const,指向的物件是乙個const

void

newf

(unsigned

char x)

// [error] two or more data types in declaration of 'newf'

void

newf

(int y)

void

ff(tokens t)

voidff(

int x)

voidff(

short y)

void

manip

(double x)

void

manip

(long x)

void

maip

(float y)

intmain()

C 過載函式(函式匹配與實參型別轉換)

1,函式匹配 過載確定 過載確定的三個步驟 1 候選函式 2 選擇可行函式 3 尋找最佳匹配 比較特殊的是print 3.14 它的引數是double型別,但是選擇候選函式的時候,因為沒有最完美匹配,只能丟失精度,強制轉換為int型別。還有print 3.2,1 這種型別,因為目標函式不唯一,有二義...

c 過載自動型別轉換

c 裡有一些隱式型別轉換,比如 double附值給int,內容會被截斷 int和double型別的值進行運算,int自動轉換為double型等。所謂過載自動型別轉換,就是使用者自己定義類的自動轉換方式。我越來越懶了,直接貼 吧。operator type 可以過載多個 operator type1 ...

C 運算子過載和型別轉換函式

c 中對於使用者自定義類物件的運算,要進行過載操作。如 class overload overload o1,o2 int num 要實現的加法操作如下 1 overload o3 o1 o2 2 overload o4 o1 任意int型 3 overload o5 任意int型 o2 4 num...