public 函式 C 的this函式和析構函式

2021-10-12 06:33:15 字數 1312 閱讀 2419

你可以因為現任不好而分手,但千萬不要認為別人更好,永遠有人更好,眼下便是更好。。。

----  網易雲熱評

一、this函式

對於普通的成員函式,this指向呼叫該函式的物件

對於建構函式,this指向正在建立的物件

#include using namespace std;class teacher*/    //通過this區分函式的形參和成員變數    teacher(const string& m_name,int m_age)    void print(void)/*編譯處理後變成類似如下:    void print(teacher* this)*/private:    string m_name;    int m_age;};int main(void)
#include using namespace std;class counter    void print(void)    counter& add(void)    void destroy(void)private:    int m_count;};int main(void)
#include using namespace std;class student;class teacher;class student;void teacher::edudate(student* s)void teacher::reply(const string& answer)void student::ask(    const string & question,teacher* t)int main(void)
二、析構函式,主要負責清理物件生命週期中的動態資源,當物件被銷毀時,該類的析構函式自動被呼叫和執行

指向public成員函式的指標

c 中,所謂普通成員函式,無非就是比普通函式在形參列表的最後,多了乙個這個型別的this指標,本質上和一般的函式沒有什麼不同。而類的靜態成員函式,則和普通函式完全一樣。所以宣告函式指標時,對於類的靜態成員函式,方法和普通函式指標是一樣的。對於普通成員函式,則有一點區別。受this指標的影響,類的非靜...

用private和public封裝函式

def private 1 name return hello,s name def private 2 name return hi,s name def greeting name if len name 3 return private 1 name else return private 2...

C 路途之 類(1) public

前陣子無論是在看c 的時候,就看到了乙個class 以為是結構體別名 真是狗眼不識 類 相當於結構體思想,但是更加的明確,也可以抽象的理解為函式套函式 但是更加的精確,複雜的多 包括了 public,private,protected,internal 這是四類訪問修飾符,大概的感覺就是,publi...