深入理解c++拷贝构造=和explicit

深入理解拷贝构造

#include <iostream>
using namespace std;

class A {
public:
	explicit A(int a){}
};

int main() {
	A a = 3;
	return 0;
}

会出现如下错误:

main.cpp: In function ‘int main()’:
main.cpp:22:15: error: conversion from ‘int’ to non-scalar type ‘A’ requested
   22 |         A a = 3;

背后原因:

A a = 3;
上面这句中,由于explicit的存在,只能使用a(3)这种构造。

根据官网案例:

struct A
{
    A(int) { }      // converting constructor
    A(int, int) { } // converting constructor (C++11)
    operator bool() const { return true; }
};
 
struct B
{
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};
 
int main()
{
    A a1 = 1;      // OK: copy-initialization selects A::A(int)
    A a2(2);       // OK: direct-initialization selects A::A(int)
    A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
    A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
    A a5 = (A)1;   // OK: explicit cast performs static_cast
    if (a1) { }    // OK: A::operator bool()
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
    bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization
 
//  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
    B b2(2);       // OK: direct-initialization selects B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int, int)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b2) { }    // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
 
    [](...){}(a4, a5, na1, na2, b5, nb2); // may suppress "unused variable" warnings
}

官网案例说明B b1 = 1;这句默认第1选择使用拷贝构造函数,而若加了explicit,那么只能选择第1选择,不能有其他选择构造。

上面结论有误:

#include <iostream>
 
struct B
{
    B(short) { std::cout << "B(short)" << std::endl; };
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};
 
int main()
{
    B b = 3;   // B(short)
}

对于上面代码,第1选择是调用拷贝构造函数,第2选择就调用B(short)函数,B(int)不能作为调用选择。

#include <iostream>
 
struct B
{
    B(short) { std::cout << "B(short)" << std::endl; }
    B(const B&) { std::cout << "B(const B&)" << std::endl; }
    explicit B(int) { std::cout << "B(int)" << std::endl; }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};
 
int main()
{
    B b = B(2);  // B(int)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值