面向对象的嵌入式学习总结(6)

#pragma warning(disable:4996)

#include <iostream>
#include <string.h>

using namespace std;

class Test
{
public:
    Test()
    {
        this->name = nullptr;
        cout << "Test" << endl;
    }
Test() = default; 声明无参构造函数使用系统默认生成的;
Test() = delete;不让系统默认生成无参构造函数
  Test(int num, const char* name, int age)
    {
        this->num = num;
        this->age = age;
        //this->name = name;//error;

        int len = strlen(name);
        this->name = new char[len + 1];
        strcpy(this->name, name);

        cout << "Test int char * int" << endl;
    }

    Test(int num, int age)
    {
        this->num = num;
        this->age = age;
        this->name = nullptr;
        cout << "Test int int" << endl;
    }

    Test(const Test& other)
    {
        this->num = other.num;
        this->age = other.age;
        //this->name = other.name;
        int len = strlen(other.name);
        this->name = new char[len + 1];
        strcpy(this->name, other.name);

        cout << "Test copy Test" << endl;
    }

    explicit Test(int num)  //防止发生隐式转换
    {
        this->num = 0;
        this->age = 0;
        this->name = nullptr;
        cout << "Test int" << endl;
    }

    explicit Test(const char* name)
    {
        this->name = nullptr;
        this->num = 0;
        this->age = 0;
    }

    Test(Test&& other)//移动拷贝拷贝构造函数
    {
        this->num = other.num;
        this->age = other.age;
        this->name = other.name;
        other.name = nullptr;

        cout << "Test copy Test &&" << endl;
    }

    ~Test()
    {
        if (name != nullptr)
        {
            delete[] name;
        }

        cout << "~Test" << endl;
    }

    int num;
    char* name;//string name;
    int age;
};

无参构造函数:系统默认生成、自定义 (default、delete)

有参构造函数:可以重载;

拷贝构造函数(用已有的对象初始化新的对象):默认拷贝构造函数(问题:浅拷贝问题) 自定义拷贝构造函数(深拷贝)
拷贝构造函数的调用时机:

  1. 用已有的对象初始化新的对象
  2. 传参:当形参为类的对象时,传参时会调用拷贝构造函数;
  3. 函数返回值为对象时;

类型转换构造函数:构造函数只有一个参数的,都是类型转换构造函数;用explicit防止发生类型转换;

移动拷贝构造函数: 解决临时对象拷贝效率问题;

void test1(Test t)
{
    cout << t.name << endl;
}

Test test2()
{
    Test temp(1, "lisi", 24);
    return temp;
}

int main()
{
#if 0
    Test t;                     //调用无参构造函数:默认的无参构造函数(类里没有任何构造函数时,系统会默认生成)
    Test t1(1, "zhangsan", 21); //有参构造函数
    Test t2(2, 23);

    Test t3(t1);
    //cout << t3.num << " " << t3.name << endl;

    //Test t4 = 1; //调用类型转换构造函数! 危险(隐式类型转换:其他可以转换成类类型)
    Test t5(1);

    //Test t6 = "hello world";
    Test t7;
    //t7 = 1;
#endif

    Test t1(1, "zhangsan", 24);

    test1(t1);

    Test t2 = test2();

    return 0;
}



#include <iostream>
#include <string>
#include <string.h>

using namespace std;

class MyString
{
public:
    MyString(const char *ptr)
    {
        int len = strlen(ptr);
        this->ptr = new char[len + 1];
        strcpy(this->ptr,ptr);
    }

    int size()
    {
        return strlen(ptr);
    }

    int at(int index)
    {
        return ptr[index];
    }

    const char * c_str()
    {
        return ptr;
    }

    
private:
    char *ptr;
};


int main()
{
    string s1('a',100);
    string s2("hello world");
    string s3(s2);
    s1.size();

    const char *temp = s2.c_str();

    MyString mys1("hello world");
    cout << mys1.at(1) << endl;

    temp = mys1.c_str();
    
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值