Chinaunix首页 | 论坛 | 博客
  • 博客访问: 744363
  • 博文数量: 239
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1045
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-22 18:25
文章分类

全部博文(239)

文章存档

2019年(9)

2018年(64)

2017年(2)

2016年(26)

2015年(30)

2014年(41)

2013年(65)

2012年(2)

分类: C/C++

2014-01-21 14:10:50

      When a  using-declaration brings names from a base class into a derived class scope, member functions and member function templates in the derived class override and/or hide member functions and member function  templates  with  the  same  name,  parameter-type-list  (8.3.5),  cv-qualification,  and  ref-qualifier (if  any)  in  a  base class (rather than conflicting).  ( ISO/IEC 14882:2011(E)  § 7.3.3 )

    Example:

点击(此处)折叠或打开

  1. #include <iostream>

  2. using namespace std;

  3. struct B
  4. {
  5.     virtual void f(int)
  6.     {
  7.         cout<<"struct B member[virtual void f(int)] called"<<endl;
  8.     };

  9.     virtual void f(char)
  10.     {
  11.         cout<<"struct B member[virtual void f(char)] called"<<endl;
  12.     };

  13.     void g(int)
  14.     {
  15.         cout<<"struct B member[void g(int)] called"<<endl;
  16.     };

  17.     void h(int)
  18.     {
  19.         cout<<"struct B member[void h(int)] called"<<endl;
  20.     };
  21. };

  22. struct D : B
  23. {
  24.     using B::f;
  25.     void f(int) // OK: D::f(int) overrides B::f(int);
  26.     {
  27.         cout<<"struct D member[void f(int)] called"<<endl;
  28.     };

  29.     using B::g;
  30.     void g(char) // OK
  31.     {
  32.         cout<<"struct D member[void g(char)] called"<<endl;
  33.     };

  34.     using B::h;
  35.     void h(int) // OK: D::h(int) hides B::h(int)
  36.     {
  37.         cout<<"struct D member[void h(int)] called"<<endl;
  38.     };
  39. };

  40. void k(D* p)
  41. {
  42.     p->f(1); // calls D::f(int)
  43.     p->f('a'); // calls B::f(char)
  44.     p->g(1); // calls B::g(int)
  45.     p->g('a'); // calls D::g(char)
  46.     p->h(1); // calls D::h(int)
  47. }

  48. int main(int argc, char* argv[])
  49. {
  50.     D test;

  51.     k(&test);

  52.     return 0;
  53. }

result:

点击(此处)折叠或打开

  1. struct D member[void f(int)] called
  2. struct B member[virtual void f(char)] called
  3. struct B member[void g(int)] called
  4. struct D member[void g(char)] called
  5. struct D member[void h(int)] called

     For  the  purpose  of  overload  resolution,  the  functions  which  are  introduced  by  a  using-declaration  into  a derived class will be treated as though they were members of the derived class.  In particular, the implicit this  parameter  shall  be  treated  as  if  it  were  a  pointer  to  the  derived  class  rather  than  to  the  base  class. This has no effect on the type of the function, and in all other respects the function remains a member of  the base class.   

     The access rules for inheriting constructors are speci?ed in 12.9; otherwise all instances of the name mentioned  in a  using-declaration shall be accessible.  In particular, if a derived class uses a  using-declaration to access  a  member  of  a  base  class,  the  member name shall  be accessible.  If  the  name  is  that  of  an overloaded member  function,  then  all  functions  named  shall  be accessible.  The  base  class  members  mentioned  by  a using-declaration shall be visible in the scope of at least one of the direct base classes of the class where the using-declaration is speci?ed. 
[ Note:  Because a  using-declaration designates a base class member (and not a member subobject or a member function of a base class subobject), a using-declaration cannot be used to resolve inherited member ambiguities.  For example,

            struct   A {  int  x();  };
            struct   B :  A {  };
            struct   C :  A {
              using   A::x;
               int  x(int);
            };

            struct   D :  B,  C {
              using   C::x;
               int  x(double);
            };
            int  f(D*  d)  {
              return   d->x();       // ambiguous:  B::x  or  C::x
            }

 — end note ]

    The  alias  created  by  the  using-declaration  has  the  usual  accessibility  for  a  member-declaration.   [ Note:  A using-declaration  that  names  a  constructor  does  not  create  aliases;  see  12.9 for  the  pertinent  accessibility  rules.   — end note ]
[ Example:

            class   A {
            private:
                 void  f(char);
            public:
                 void  f(int);
            protected:
                 void  g();
            };

            class   B :  public  A  {
              using   A::f;          // error:  A::f(char)  is inaccessible
            public:
              using   A::g;          // B::g  is a public synonym for  A::g
            };

— end example ]


阅读(867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~