Android NDK基础19:C++_构造函数属性初始化_new(delete)_静态成员_this指针

构造函数属性初始化

//构造函数的属性初始化列表
class Teacher {
private:
    char* name;
public:
    Teacher(char* name) {
        this->name = name;
        cout << "Teacher有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }

    char* getName() {
        return this->name;
    }
};

class Student {
private:
    int id;
    //属性对象
    //Teacher t = Teacher("Joe");
    Teacher t1;
    Teacher t2;
public:
    Student(int id, char *t1_name, char* t2_name) : t1(t1_name), t2(t2_name) {
        this->id = id;
        cout << "Student有参构造函数" << endl;
    }

    void print() {
        cout << id << "," << t1.getName() <<"," << t2.getName() << endl;
    }

    ~Student() {
        cout << "Student析构函数" << endl;
    }
};

void func() {
    //Teacher构造 -> Student构造 -> Student析构 -> Teacher析构
    Student s1(10, "Miss Zhao", "Mr Qian");
    //Student s2(20, "Mr Sun", "Miss Li");
    s1.print();
    //s2.print();
}

void main() {
    func();

    system("pause");
}

new(delete)动态内存分配

  • C++ 通过new(delete)动态内存分配
  • C 通过malloc(free)动态内存分配
class Teacher {
private:
    char* name;
public:
    Teacher(char* name) {
        this->name = name;
        cout << "Teacher有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }

    void setName(char* name) {
        this->name = name;
    }

    char* getName() {
        return this->name;
    }
};

void func() {
    //C++    
    //会调用构造和析构函数
    Teacher *t1 = new Teacher("Joe");
    cout << t1->getName() << endl;
    //释放
    delete t1;

    //C
    //不会调用构造和析构函数
    //Teacher *t2 = (Teacher*) malloc(sizeof(Teacher));
    //t2->setName("Joe");
    //free(t2);
}

void main() {
    func();
    //数组类型
    //C
    //int *p1 = (int*) malloc(sizeof(int) * 10);
    //p1[0] = 1;
    //free(p1);

    //C++
    int *p2 = new int[10];
    p2[0] = 2;
    //释放数组 []
    delete[] p2;

    system("pause");
}

static 静态属性和方法

//static 静态属性和方法
class Teacher {
public:
    char* name;
    //计数器
    static int total;
public:
    Teacher(char* name) {
        this->name = name;        
        cout << "Teacher有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }

    void setName(char* name) {
        this->name = name;
    }

    char* getName() {
        return this->name;
    }

    //计数,静态函数
    static void count() {
        total++;        
        cout << total << endl;
    }
};

//静态属性初始化赋值
int Teacher::total = 9;

void main() {
    Teacher::total++;
    cout << Teacher::total << endl;
    //直接通过类名访问
    Teacher::count();

    //也可以通过对象名访问
    Teacher t("Joe");
    t.count();

    system("pause");
}

this指针

//this,当前对象的指针
//函数是共享的,必须要有能够标识当前对象是谁的办法
class Teacher {
private:
    char* name;
    int age;
public:
    Teacher(char* name,int age) {
        this->name = name;
        this->age = age;
        cout << "Teacher有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "Teacher析构函数" << endl;
    }

    //常函数,修饰的是this
    //既不能改变指针的值,又不能改变指针指向的内容
    //const Teacher* const this
    void print() const {
        printf("%#x\n", this);
        //不能改变属性的值
        //this->name = "Mr Ye";
        //不能改变this指针的值
        //this = (Teacher*)0x000008;
        cout << this->name << "," << this->age << endl;
    }

    void normal_print() {        
        cout << this->name << "," << this->age << endl;        
    }
};

void main() {
    Teacher t1("Joe", 20);
    const Teacher t2("Ann", 18);
    //t2.normal_print(); 常量对象只能调用常量函数,不能调用非常量函数
    //常函数,当前对象不能被修改,防止数据成员被非法访问
    printf("%#x\n", &t1);
    t1.print();

    printf("%#x\n", &t2);
    t2.print();

    system("pause");
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/android-ndk-basic-cpp-initialize-constructor-properties-new-delete-static-member-this-pointer/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android NDK基础19:C++_构造函数属性初始化_new(delete)_静态成员_this指针
构造函数属性初始化 //构造函数的属性初始化列表 class Teacher { private: char* name; public: Teacher(char* name) { this->name = na……
<<上一篇
下一篇>>
文章目录
关闭
目 录