C++ - 带参构造函数,拷贝构造函数

1.带有参数的构造函数

-> 构造函数可以根据需要定义参数。

->  一个类中可以存在多个重载的构造函数。

->  构造函数的重载遵循C++重载的规则。

#include <stdio.h>

class Test
{
public:
    Test() 
    { 
        printf("Test()\n");
    }
    Test(int v) 
    { 
        printf("Test(int v), v = %d\n", v);
    }
};

int main()
{
    Test t;      // 调用 Test()
    Test t1(1);  // 调用 Test(int v)
    Test t2 = 2; // 调用 Test(int v)

    int i(100);

    printf("i = %d\n", i);

    return 0;
}

2.构造函数的手动调用

->  一般情况下,构造函数在对象定义时被自动调用。

->  一些特殊情况下,需要手工调用构造函数。

#include <stdio.h>

class Test
{
private:
    int m_value;
public:
    Test() 
    { 
        printf("Test()\n");
        
        m_value = 0;
    }
    Test(int v) 
    { 
        printf("Test(int v), v = %d\n", v);
        
        m_value = v;
    }
    int getValue()
    {
        return m_value;
    }
};

int main()
{
    Test ta[3] = {Test(), Test(1), Test(2)};   //执行了三次构造函数   
    
    for(int i=0; i<3; i++)
    {
        printf("ta[%d].getValue() = %d\n", i , ta[i].getValue());//0,1,2
    }
    
    Test t = Test(100);
    
    printf("t.getValue() = %d\n", t.getValue());//100
    
    return 0;
}

3.特殊的构造函数->拷贝构造函数

自己手工编写一个拷贝构造函数,居然编译不过。
编译器觉得我们自己提供一个拷贝构造函数,拷贝构造函数也是构造函数,所以编译器觉得无需给我们提供构造函数了,所以就会编译错误。

#include <stdio.h>

class Test
{
private:
    int i;
    int j;
public:
    int getI()
    {
        return i;
    }
    int getJ()
    {
        return j;
    }
    /*Test(const Test& t)//自己手工编写一个拷贝构造函数,居然编译不过。
    {                    //编译器觉得我们自己提供一个拷贝构造函数,拷贝构造函数
	                     //也是构造函数,所以编译器觉得无需给我们提供构造函数了
        i = t.i;         //所以就会编译错误。下面再编写无参的构造函数就不会出错。
        j = t.j;
    }
    Test()
    {
    }*/
};

int main()
{
    Test t1;
    Test t2 = t1;
    
    printf("t1.i = %d, t1.j = %d\n", t1.getI(), t1.getJ());//t1.i=134513984, t1.j=-1079264296
    printf("t2.i = %d, t2.j = %d\n", t2.getI(), t2.getJ());//t1.i=134513984, t1.j=-1079264296
    
    return 0;
}

这里写图片描述

4.拷贝构造函数 

浅拷贝:拷贝后对象的物理状态相同。

深拷贝:拷贝后对象的逻辑状态相同。

注意:编译器提供的拷贝构造函数只进行浅拷贝!

代码示例:

#include <stdio.h>

class Test
{
private:
    int i;
    int j;
    int* p;
public:
    int getI()
    {
        return i;
    }
    int getJ()
    {
        return j;
    }
    int* getP()
    {
        return p;
    }
    Test(const Test& t)
    {
        i = t.i;
        j = t.j;
        p = new int;

        *p = *t.p;
    }
    Test(int v)
    {
        i = 1;
        j = 2;
        p = new int;

        *p = v;
    }
    void free()
    {
        delete p;
    }
};

int main()
{
    Test t1(3);
    Test t2(t1);

    printf("t1.i = %d, t1.j = %d, *t1.p = %d\n", t1.getI(), t1.getJ(), *t1.getP());
    printf("t2.i = %d, t2.j = %d, *t2.p = %d\n", t2.getI(), t2.getJ(), *t2.getP());

    t1.free();
    t2.free();

    return 0;
}

简图分析:

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嵌入式_笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值