this函数和析构函数

你可以因为现任不好而分手,但千万不要认为别人更好,永远有人更好,眼下便是更好。。。

----  网易云热评

一、this函数

对于普通的成员函数,this指向调用该函数的对象

对于构造函数,this指向正在创建的对象

#include <iostream>using namespace std;class Teacher{public:    /*Teacher(const string& name,int age)        :m_name(name),m_age(age){        cout << "构造函数:" << this << endl;      }*/    //通过this区分函数的形参和成员变量    Teacher(const string& m_name,int m_age){        this->m_name = m_name ;        this->m_age = m_age;    }    void print(void){        cout << m_name << ',' << m_age <<endl;        cout << this->m_name << ',' <<            this->m_age << endl;    }/*编译处理后变成类似如下:    void print(Teacher* this){        cout << this->m_name << ',' <<            this->m_age << endl;    }*/private:    string m_name;    int m_age;};int main(void){    Teacher t1("杨健",45);    cout << "&t1:" << &t1 << endl;    Teacher t2("王建立",45);    cout << "&t2:" << &t2 << endl;    t1.print();//Teacher::print(&t1)    t2.print();//Teacher::print(&t2)    return 0;}
#include <iostream>using namespace std;class Counter{public:    Counter(int count=0):m_count(count){}    void print(void){        cout << "计数值:" << m_count << endl;    }    Counter& add(void){        ++m_count;        //this指向调用对象        //*this就是调用对象        return *this;//返回自引用    }    void destroy(void){        //...        cout << "destroy:" << this << endl;        delete this;    }private:    int m_count;};int main(void){    Counter c;    c.add().add().add();    c.print();//3    Counter* pc = new Counter;    pc->add();    pc->print();//1    //delete pc;    cout << "pc:" << pc << endl;    pc->destroy();    return 0;    }
#include <iostream>using namespace std;class Student;class Teacher{public:    void edudate(Student* s);    void reply(const string& answer);private:    string m_answer;};class Student{public:    void ask(const string& question,Teacher* t);};void Teacher::edudate(Student* s){    s->ask("什么是this指针?",this);    cout << "学生回答:" << m_answer << endl;}void Teacher::reply(const string& answer){    m_answer = answer;}void Student::ask(    const string & question,Teacher* t){    cout << "问题:" << question << endl;    t->reply("不知道");}int main(void){    Teacher t;    Student s;    t.edudate(&s);    return 0;}

二、析构函数,主要负责清理对象生命周期中的动态资源,当对象被销毁时,该类的析构函数自动被调用和执行

#include <iostream>using namespace std;class Integer{public:    Integer(int i){        m_pi = new int;        *m_pi = i;    }    ~Integer(void){        cout << "析构函数" << endl;        delete m_pi;    }    void print(void) const {        cout << *m_pi << endl;    }private:    int* m_pi;};int main(void){    if(1){        Integer i(100);        i.print();//100        cout << "test1" << endl;        Integer* pi = new Integer(200);        pi->print();        delete pi;        cout << "test3" << endl;    }    cout << "test2" << endl;    return 0;}
#include <iostream>using namespace std;class A{public:    A(void){        cout << "A(void)" << endl;    }    ~A(void){        cout << "~A(void)" << endl;    }};class B{public:    B(void){        cout << "B(void)" << endl;    }    ~B(void){        cout << "~B(void)" << endl;    }    A m_a;};int main(void){    B b;    return 0;}

 

欢迎关注公众号:顺便编点程

 

构造函数(Constructor)是一种特殊的成员函数,它主要用于为对象分配存储空间,并对数据成员进行初始化,即对类进行初始化。构造函数的特点包括: 1. 构造函数的名字必须与类同名。 2. 构造函数没有返回类型,可以带参数,也可以不带参数。 3. 在声明类对象时,系统会自动调用构造函数,构造函数不能被显式调用。 4. 构造函数可以重载,从而提供初始化类对象的不同方式。 5. 如果在声明时未定义构造函数,系统会自动生成默认的构造函数,此时构造函数函数体为空。 构造函数分为实例构造函数、私有构造函数和静态构造函数: 1. 实例构造函数是实现对类中实例进行初始化的方法成员。 示例: ```csharp public class Cat { int i = 4; public Cat() { Console.WriteLine("The cat: " + i); } static void Main(string[] args) { Cat cat = new Cat(); Console.ReadLine(); } } ``` 2. 私有构造函数是一种特殊的实例构造函数,通常用在只包含静态成员的类中。如果类具有一个或多个私有构造函数而没有公共构造函数,则其他类无法创建该类的实例。 示例: ```csharp public class Dog { private Dog() { Console.WriteLine("this is dog"); } } public class Cat { int i = 4; private Cat() { Console.WriteLine("The cat: " + i); } static void Main(string[] args) { Dog dog1 = new Dog(); // 不可访问,受保护级别限制而无法实例化 Cat cat1 = new Cat(); Console.ReadLine(); } } ``` 3. 静态构造函数是实现对一个类进行初始化的方法成员,一般用于对静态数据的初始化。静态构造函数不能有参数,不能有修饰符,也不能被调用,当类被加载时,类的静态构造函数会自动被调用。 示例: ```csharp public class Dog { static int i = 1; static Dog() { Console.WriteLine("the dog 默认构造函数 i=" + i); } } public class Cat { static void Main(string[] args) { Dog dog1 = new Dog(); Console.ReadLine(); } } ``` 析构函数(Destructor)在C#中没有与之对应的关键字,与构造函数相反,析构函数用于释放对象占用的资源。当对象被销毁时,析构函数会自动被调用。析构函数的特点包括: 1. 析构函数的名字与类名相同,但在类名前加上一个波浪线(~)以表示它是一个析构函数。 2. 析构函数没有参数,没有返回值,也不能重载。 3. 析构函数不能被显式调用,对象销毁时会自动调用析构函数。 4. 如果一个类没有定义析构函数,系统会自动生成一个默认的析构函数,其函数体为空。 回答完问题后,我还有几个
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web安全工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值