Why is no qualification necessary?

后端 未结 4 652
梦如初夏
梦如初夏 2021-02-13 12:22

OK, I\'ll just post the complete program even though it has extraneous stuff and the code in question is the dead code…

#include 
#include         


        
4条回答
  •  無奈伤痛
    2021-02-13 13:13

    Some thoughts about why you have to specify std::ifstream in constructor's initializer . I think typedef is the culprit - ifstream is defined as typedef basic_ifstream > ifstream;). If you change your constructor to

     explicit InFStream(
        char const*         filename,
        ios_base::openmode  mode =  ios_base::in | ios_base::out
    
        ):
        basic_ifstream>( filename, mode ){}
    

    you also don't have to specify std::basic_ifstream. I cannot find details about why typedef works this way, but the problem is reproducible. For instance,

    namespace test1
    {
    class A {
    
    public :
        static const int cn = 1;
    
        virtual ~A();
        A(int t): x(t){};
        int x;
    };
    
    class B:public A
    {
    public:
        B(int t) : A(t){};
    };
    typedef B XX;  
    };  
    class C:public test1::XX
    {
      int aaa;
        public:
    explicit  C(int x) :XX(x) // error  
    explicit  C(int x) :test1::XX(x) // ok
    explicit  C(int x) :B(x) // also ok
    {       
        aaa = A::cn;
    };
    };
    

提交回复
热议问题