Android NDK基础18:C++_构造函数_析构函数_拷贝构造函数

函数

函数默认参数

//函数默认参数
void print(int x, int y = 6, int z = 8) {
    cout << x << endl;
}

//重载
void print(int x, bool ret) {
    cout << x << endl;
}

void main() {
    print(20);

    system("pause");
}

可变参数

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <iostream>
#include <stdarg.h>

//可变参数
//Java可变参数:int...
void func(int i, ...) {
    //可变参数指针
    va_list args_p;
    //开始读取可变参数,i是最后一个固定参数
    va_start(args_p, i);
    int a = va_arg(args_p, int);
    char b = va_arg(args_p, char);
    int c = va_arg(args_p, int);
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    //结束
    va_end(args_p);
}

void main() {
    func(9, 20, 'b', 30);

    system("pause");
}
//循环读取可变参数
void func(int i, ...) {
    //可变参数指针
    va_list args_p;
    //开始读取可变参数,i是最后一个固定参数
    va_start(args_p, i);
    int value;
    while (1) {
        value = va_arg(args_p, int);
        if (value <= 0) {
            break;
        }
        cout << value << endl;
    }

    //结束
    va_end(args_p);
}

void main() {
    func(10, 1, 2, 3);

    system("pause");
}

C++类

C++类的普遍写法

#include "MyTeacher.h"

void main() {
    MyTeacher t;
    t1.name = "Jack";
    t1.age = 20;

    cout << t.getName() << endl;

    system("pause");
}
#pragma once

class MyTeacher {
public:
    int age;
    char* name;
public:
    void setAge(int age);
    int getAge();
    void setName(char* name);
    char* getName();
};
#include "MyTeacher.h"

void MyTeacher::setAge(int age) {
    this->age = age;
}

int MyTeacher::getAge(){
    return this->age;
}

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

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

构造函数

class Teacher {
private:
    char *name;
    int age;
public:
    //无参构造函数(重写无参构造函数,就会覆盖默认的无参构造函数)
    Teacher() {
        cout << "无参构造函数" << endl;
    }

    //有参构造函数
    Teacher(char *name, int age) {
        this->name = name;
        this->age = age;
        cout << "有参构造函数" << endl;
    }    
};

void main() {
    //Teacher t1;
    Teacher t2("Joe", 20);

    //另外一种调用方式
    Teacher t3 = Teacher("Joe", 20);

    system("pause");
}

析构函数

//析构函数
class Teacher {
private:
    char *name;
    int age;
public:
    //无参构造函数赋默认值
    Teacher() {
        this->name = (char*)malloc(100);
        strcpy(name, "AndroidiOS.cc");
        age = 20;
        cout << "无参构造函数" << endl;
    }
    //析构函数
    //当对象要被系统释放时,析构函数被调用
    //作用:善后处理
    ~Teacher() {
        cout << "析构函数" << endl;
        //释放内存
        free(this->name);
    }
};

void func() {
    Teacher t;  //func函数执行完毕调用析构函数
}

void main(){
    func();

    system("pause");
}

拷贝构造函数

//拷贝构造函数
class Teacher {
private:
    char *name;
    int age;
public:
    //有参构造函数会覆盖默认的构造函数
    Teacher(char *name, int age){
        this->name = name;
        this->age = age;
        cout << "有参构造函数" << endl;
    }

    //拷贝构造函数(值拷贝)
    //默认拷贝构造函数,就是值拷贝
    Teacher(const Teacher &obj) {
        this->name = obj.name;
        this->age = obj.age;
        cout << "拷贝构造函数" << endl;
    }

    void print() {
        cout << name << "," << age << endl;
    }
};

Teacher func(Teacher t) {
    t.print();
    return t;
}

void main() {
    Teacher t1("Joe", 20);

    //拷贝构造函数被调用的场景
    //1.声明时赋值
    //Teacher t2 = t1;
    //t2.print();
    //2.作为参数传入,实参给形参赋值
    func(t1);
    //3.作为函数返回值返回,给变量初始化赋值
    //Teacher t3 = func(t1);

    //这里不会被调用
    //Teacher t1 ;
    //Teacher t2;
    //t1 = t2;

    system("pause");
}

浅拷贝(值拷贝)问题

//浅拷贝(值拷贝)问题
class Teacher {
private:
    char *name;
    int age;
public:
    Teacher(char *name, int age) {
        this->name = (char*)malloc(100);
        strcpy(this->name, name);
        this->age = age;
        cout << "有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "析构函数" << endl;
        //释放内存
        free(this->name);
    }

    void print() {
        cout << name << "," << age << endl;
    }
};

void func() {
    Teacher t1("Joe", 20);

    Teacher t2 = t1;
    t2.print();
}

void main() {
    func();

    system("pause");
}

深拷贝

  • 浅拷贝(值拷贝):拷贝的是指针地址
  • 深拷贝:拷贝的是指针指向的堆内存空间数据内容
//深拷贝
class Teacher {
private:
    char *name;
    int age;
public:
    Teacher(char *name, int age) {
        int len = strlen(name);
        this->name = (char*) malloc(len+1);
        strcpy(this->name, name);
        this->age = age;
        cout << "有参构造函数" << endl;
    }

    ~Teacher() {
        cout << "析构" << endl;
        //释放内存
        free(this->name);
    }

    //深拷贝
    Teacher(const Teacher &obj) {
        //复制name属性
        int len = strlen(obj.name);
        this->name = (char*) malloc(len+1);
        strcpy(this->name, obj.name);
        this->age = obj.age;
    }

    void print() {
        cout << name << "," << age << endl;
    }
};

void func() {
    Teacher t1("Joe", 20);

    Teacher t2 = t1;
    t2.print();
}

void main() {
    func();

    system("pause");
}

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

THE END
分享
二维码
打赏
海报
Android NDK基础18:C++_构造函数_析构函数_拷贝构造函数
函数 函数默认参数 //函数默认参数 void print(int x, int y = 6, int z = 8) { cout << x << endl; } //重载 void print(int x, bool ret)……
<<上一篇
下一篇>>
文章目录
关闭
目 录