【问题标题】:is there any easy way to expose methods of private parent class c++有什么简单的方法可以公开私有父类c ++的方法
【发布时间】:2010-02-28 16:02:53
【问题描述】:

有没有办法直接暴露私有父类的一些方法。 在下面的示例中,如果我有一个 Child 类型的对象,我希望能够直接调用其父级的方法 a(),而不是 b(); 当前的解决方案会产生大量样板代码,尤其是在存在大量参数的情况下。

class Parent {
    public:
        void a(int p1, double p2, int p3, std::vector <int> &p4);
        void b();
    };
class Child : private Parent {
    public:
        void a(int p1, double p2, int p3, std::vector <int> &p4) {
            Parent::a(p1, p2, p3, p4);
            }
   };

【问题讨论】:

    标签: c++


    【解决方案1】:

    你可以使用 using 声明。

    class Child : private Parent {
        public:
            using Parent::a;
       };
    

    【讨论】:

      【解决方案2】:

      这可能会有所帮助:http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.6

      class Child : protected Parent
      {
        public:
          using Parent::a;
      }
      

      编辑:添加public

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-23
        • 2011-12-08
        相关资源
        最近更新 更多