using 注意事项

  • 取代 typedef 定义变量的功能.
typedef int int_t; 
using char_t = char;
  • 修饰 namespace, 引入命名空间.
  namespace ns {
      void show(void) {}
  }

  using namespace ns;
  int main(void) {
      show();
  }
  • 改变继承的访问权限控制.
  class base {
      public:
          void show(void) {}
      protected:
          void add(void) {}
  };

  class derive: private base {
      public:
          using base::show;
      protected:
          using base::add;
  };
  • 改变继承中函数重载的默认规则.
  class base {
    public:
      void show(void) { std::cout << "base" << std::endl; }
      void show(int a) { std::cout << "base: " << a << std::endl; }

  };

  class derive : private base {
    public:
      using base::show;
      void show(int a) { std::cout << "derive: " << a << std::endl; }

  };

  int main(void) {
      derive d;
      d.show(); // 输出: base
      d.show(1); //输出: derive: 1
  }

如果觉得有帮助,可以扫描右边的微信打赏码支持一下.

Leave a Reply

Your email address will not be published. Required fields are marked *