zoukankan      html  css  js  c++  java
  • Using-declaration

    Two function:

    In namespace and block scope

    Using-declaration introduces a member of another namespace into current namespace or block scope

    #include <iostream>
    #include <string>
    using std::string;
    int main()
    {
        string str = "Example";
        using std::cout;
        cout << str;
    }

    In class definition

    Using-declaration introduces a member of a base class into the derived class definition, such as to expose a protected member of base as public member of derived. In this case, nested-name-specifier must name a base class of the one being defined. If the name is the name of an overloaded member function of the base class, all base class member functions with that name are introduced. If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.

    #include <iostream>
    struct B {
        virtual void f(int) { std::cout << "B::f
    "; }
        void g(char)        { std::cout << "B::g
    "; }
        void h(int)         { std::cout << "B::h
    "; }
     protected:
        int m; // B::m is protected
        typedef int value_type;
    };
     
    struct D : B {
        using B::m; // D::m is public
        using B::value_type; // D::value_type is public
     
        using B::f;
        void f(int) { std::cout << "D::f
    "; } // D::f(int) overrides B::f(int)
        using B::g;
        void g(int) { std::cout << "D::g
    "; } // both g(int) and g(char) are visible
                                               // as members of D
        using B::h;
        void h(int) { std::cout << "D::h
    "; } // D::h(int) hides B::h(int)
    };
     
    int main()
    {
        D d;
        B& b = d;
     
    //    b.m = 2; // error, B::m is protected
        d.m = 1; // protected B::m is accessible as public D::m
        b.f(1); // calls derived f()
        d.f(1); // calls derived f()
        d.g(1); // calls derived g(int)
        d.g('a'); // calls base g(char)
        b.h(1); // calls base h()
        d.h(1); // calls derived h()
    }

    Inheriting constructors

    If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using Base::Base;), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.

    If overload resolution selects an inherited constructor, it is accessible if it would be accessible when used to construct an object of the corresponding base class: the accessibility of the using-declaration that introduced it is ignored.

    If overload resolution selects one of the inherited constructors when initializing an object of such derived class, then the Base subobject from which the constructor was inherited is initialized using the inherited constructor, and all other bases and members of Derived are initialized as if by the defaulted default constructor (default member initializers are used if provided, otherwise default initialization takes place). The entire initialization is treated as a single function call: initialization of the parameters of the inherited constructor is sequenced-before initialization of any base or member of the derived object.

    struct B1 {  B1(int, ...) { } };
    struct B2 {  B2(double)   { } };
     
    int get();
     
    struct D1 : B1 {
      using B1::B1;  // inherits B1(int, ...)
      int x;
      int y = get();
    };
     
    void test() {
      D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
                     // then d.x is default-initialized (no initialization is performed),
                     // then d.y is initialized by calling get()
      D1 e;          // Error: D1 has no default constructor
    }
     
    struct D2 : B2 {
      using B2::B2; // inherits B2(double)
      B1 b;
    };
     
    D2 f(1.0);       // error: B1 has no default constructor
  • 相关阅读:
    怎么接音响
    怎样区分音箱与音响
    什么是卡盟
    小白晋级达人必备 电视接口使用介绍(4)
    液晶电视插有线电视信号线的是哪个接口 HDMI是什么接口
    Google 镜像站搜集
    屏幕检测
    网站引流
    夜神安卓模拟器
    html5模拟平抛运动
  • 原文地址:https://www.cnblogs.com/yetanghanCpp/p/9258251.html
Copyright © 2011-2022 走看看