cpp——类——explicit构造函数

本文详细介绍了C++中的explicit构造函数,解释了其与implicit构造函数的区别。explicit构造函数仅支持直接初始化,不支持复制初始化,而implicit构造函数两者都支持。复制初始化在传递非类类型时与直接初始化相同,但在类类型中,复制初始化不能使用explicit构造函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

explicit只能修饰构造函数,构造函数默认implicit,implicit不是keyword
  • implicit构造函数:可用于直接初始化,也可用于复制初始化
  • explicit构造函数:可用于直接初始化,不可用于复制初始化

implicit

class CAnimal
{
public:
    CAnimal(int group) : mGroup(group)
    {
        cout << "CAnimal(" << group << ")" << endl;
    }
    
    CAnimal(const CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other) " << mGroup << endl;
    }
    
    CAnimal& operator=(const CAnimal &other)
    {
        mGroup = other.mGroup;
        cout << "CAnimal operator= " << mGroup << endl;
        return *this;
    }
    
private:
    int mGroup;
};
void implicit_construct()
{
    {
        cout << "-----init-----" << endl;
        CAnimal animal1(1);
        CAnimal animal2 = 2;
        CAnimal animal3(animal1);
        CAnimal animal4 = animal2;
    }
    
    {
        cout << "-----assign-----" << endl;
        CAnimal animal1(1);
        CAnimal animal2(2);
        animal2 = animal1;
        animal2 = 3;
    }
}
output:
-----init-----
CAnimal(1)
CAnimal(2)
CAnimal(const CAnimal &other) 1
CAnimal(const CAnimal &other) 2
-----assign-----
CAnimal(1)
CAnimal(2)
CAnimal operator= 1
CAnimal(3)
CAnimal operator= 3

explicit

class CAnimal
{
public:
    explicit CAnimal(int group) : mGroup(group)
    {
        cout << "CAnimal(" << group << ")" << endl;
    }
    
    explicit CAnimal(const CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other) " << mGroup << endl;
    }
    
    CAnimal& operator=(const CAnimal &other)
    {
        mGroup = other.mGroup;
        cout << "CAnimal operator= " << mGroup << endl;
        return *this;
    }
    
private:
    int mGroup;
};
void explicit_construct()
{
    {
        cout << "-----init-----" << endl;
        CAnimal animal1(1);
        //CAnimal animal2 = 2;
        CAnimal animal3(animal1);
        //CAnimal animal4 = animal1;
    }
    
    {
        cout << "-----assign-----" << endl;
        CAnimal animal1(1);
        CAnimal animal2(2);
        animal2 = animal1;
        //animal2 = 3;
    }
}
output:
-----init-----
CAnimal(1)
CAnimal(const CAnimal &other) 1
-----assign-----
CAnimal(1)
CAnimal(2)
CAnimal operator= 1

总结

  • CAnimal animal = value,复制初始化,如果构造函数CAnimal(value)为implicit,正确初始化,如果为explicit,编译error
  • CAnimal &other = value,初始化式期望右值value为CAnimal类型,若value为非CAnimal类型,如果构造函数CAnimal(value)为implicit,生成匿名CAnimal对象,如果为explicit,编译error
  • implicit构造函数可用于直接初始化,也可用于复制初始化
  • explicit构造函数可用于直接初始化,不可用于复制初始化
  • 复制初始化只允许传递1个参数

直接初始化VS复制初始化

非类类型

  • 非类类型直接初始化和复制初始化无区别,值复制

类类型

  • 直接初始化可使用implicit构造函数,也可使用explicit构造函数
  • 复制初始化可使用implicit构造函数,不可使用explicit构造函数
  • 复制初始化只允许传递1个参数
class CAnimal
{
public:
    CAnimal(int group, int subGroup = 1) : mGroup(group), mSubGroup(subGroup)
    {
        cout << "CAnimal(" << group << ", " << subGroup << ")" << endl;
    }
    
    CAnimal(const CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other) " << mGroup << endl;
    }
    
    CAnimal& operator=(const CAnimal &other)
    {
        mGroup = other.mGroup;
        cout << "CAnimal operator= " << mGroup << endl;
        return *this;
    }
    
private:
    int mGroup;
    int mSubGroup;
};
void construct()
{
    CAnimal animal1(5);
    CAnimal animal2(5, 8);
    CAnimal animal3 = 5;
    CAnimal animal4 = (5, 8);
}
output:
CAnimal(5, 1)
CAnimal(5, 8)
CAnimal(5, 1)
CAnimal(8, 1)
注:(5, 8)为逗号表达式,值为8,因此CAnimal animal4 = (5, 8)等价于CAnimal animal4 = 8,最终转换为CAnimal animal4(8)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值