Inheriting constructors - C++11, 23 of n

本文详细解释了C++中如何使用using声明来隐式声明一系列继承构造函数,包括非模板构造函数及构造模板,并讨论了默认参数的行为以及继承构造函数的具体案例。

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

Inheriting constructors

  • using-declaration that names a constructor implicitly declares a set of inheriting constructors. The candidate set of inherited constructors from the class X named in the using-declaration consists of actual constructors and notional constructors that result from the transformation of defaulted parameters as follows:
    • all non-template constructors of X, and
    • for each non-template constructor of X that has at least one parameter with a default argument, the set of constructors that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list, and
    • all constructor templates of X, and
    • for each constructor template of X that has at least one parameter with a default argument, the set of constructor templates that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list.
  • For each non-template constructor in the candidate set of inherited constructors [ other than a constructor having no parameters or a copy/move constructor having a single parameter],a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears. 
  • Similarly, for each constructor template in the candidate set of inherited constructors, a constructor template is implicitly declared with the same constructor characteristics unless there is an equivalent user-declared constructor template in the complete class where the using-declaration appears. 
  • Note: Default arguments are not inherited.
  • A constructor so declared has the same access as the corresponding constructor in X. It is deleted if the corresponding constructor in X is deleted
  • Examples:
    struct B1 {
        B1(int);
    };
    struct B2 {
        B2(int = 13, int = 42);
    };
    struct D1 : B1 {
        using B1::B1;  // using-declaration
    };
    struct D2 : B2 {
        using B2::B2;
    };
    The candidate set of inherited constructors in D1 for B1 is
    — B1(const B1&)
    — B1(B1&&)
    — B1(int)
    The set of constructors present in D1 is
    — D1(), implicitly-declared default constructor, ill-formed if odr-used
    — D1(const D1&), implicitly-declared copy constructor, not inherited
    — D1(D1&&), implicitly-declared move constructor, not inherited
    — D1(int), implicitly-declared inheriting constructor
    The candidate set of inherited constructors in D2 for B2 is
    — B2(const B2&)
    — B2(B2&&)
    — B2(int = 13, int = 42)
    — B2(int = 13)
    — B2()
    The set of constructors present in D2 is
    — D2(), implicitly-declared default constructor, not inherited
    — D2(const D2&), implicitly-declared copy constructor, not inherited
    — D2(D2&&), implicitly-declared move constructor, not inherited
    — D2(int, int), implicitly-declared inheriting constructor
    — D2(int), implicitly-declared inheriting constructor

    struct B1 {
        B1(int);
    };
    struct B2 {
        B2(int);
    };
    struct D1 : B1, B2 {
        using B1::B1;
        using B2::B2;
    }; // ill-formed: attempts to declare D1(int) twice
    struct D2 : B1, B2 {
        using B1::B1;
        using B2::B2;
        D2(int); // OK: user declaration supersedes both implicit declarations
    };

    template< class T >
    struct D : T {
        using T::T; // declares all constructors from class T
        ~D() { std::clog << "Destroying wrapper" << std::endl; }
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值