全域性函式VS成員函式

2022-04-29 10:33:07 字數 519 閱讀 7655

1 #include 2

3using

namespace

std;45

class

test613

intgeta()

1417

intgetb()

1821

22 test add(test &t2)

2328

void

print()

2933

protected:34

private:35

inta, b;

36};

3738 test add(test &t1, test &t2)

3943

44int

main()

45

成員函式隱藏了乙個this指標,誰呼叫成員函式,this指標就指向誰,這個this指標就是指向呼叫成員函式的物件的首位址.

在成員函式裡, *this 就是呼叫物件元素本身.一般用於return *this

全域性函式和成員函式

class test1 public test1 this,int a,int b 全域性函式形式 test1 int a 0,int b 0 this a a this b b public int a int b public 成員函式 test1 t add test1 t2 test1 t3...

c 成員函式和全域性函式的區分

include using namespace std 設計乙個立方體的類,求出立方體的面積和體積 分別用全域性函式和成員函式進行判斷兩個立方體是否相同 class cube 獲取長 intget l 設定寬 void set w int w 獲取寬 intget w 設定高 void set h ...

C 之全域性函式和成員函式互相轉換

解析 成員函式會用this指標自動隱藏第乙個運算元 左運算元 1 把全域性函式轉化成成員函式,通過this指標隱藏左運算元。test add test t1,test t2 test add test t2 2 把成員函式轉化成全域性函式,多了乙個引數 vpid printab void print...