【问题标题】:Defining a function in derived class without declaring in the base class在派生类中定义函数而不在基类中声明
【发布时间】:2014-03-21 09:33:37
【问题描述】:

我看到一些例子,比如基类函数被声明为纯函数(作为虚拟),而在派生类中它被声明(作为虚拟)并实现。在第二种情况下(我目前正在做的),基类不声明函数,只有派生类声明(不是虚拟的)和实现。这两个选项有什么区别?

【问题讨论】:

    标签: c++ derived-class


    【解决方案1】:

    在第一种情况下(顺便说一句,派生类中的虚拟声明是多余的——虚拟化是继承的),可以使用指针Base* p = new Derived() 来调用p->the function。

    在第二种情况下,您无法使用p 调用该函数。你只能使用Derived* pp = new Derived() 然后pp->the function。

    【讨论】:

      【解决方案2】:

      如果基类根本没有声明函数,则无法通过基类的类型调用该函数。示例:

      struct Base1
      {
        virtual ~Base1() {}
      
        virtual void foo() const = 0;
      };
      
      
      struct Derived1 : Base1
      {
        virtual void foo() const override { std::cout << "Foo!\n"; }
      };
      
      int main()
      {
        Base1 *p = new Derived1();
        p->foo();  // Works fine
        delete p;
      }
      

      对比

      struct Base2
      {
        virtual ~Base2() {}
      };
      
      
      struct Derived2 : Base2
      {
        virtual void foo() const { std::cout << "Foo!\n"; }
      };
      
      int main()
      {
        Base2 *p = new Derived2();
        p->foo();  // Compiler error: no foo() in Base2
        delete p;
      }
      

      【讨论】:

        【解决方案3】:

        不同之处在于,在第一种情况下你有 多态性,而在第二种情况下你没有它。

        考虑以下示例

        第一种情况

        #include <iostream>
        
        struct B
        {
           virtual void f() const = 0;
           virtual ~B() {}
        };
        
        
        struct D1
        {
           void f() const { std::cout << "It's me, D1!" << std::endl; }
        };
        
        
        struct D2
        {
           void f() const { std::cout << "No, it's me, D2!" << std::endl; }
        };
        
        
        void g( const B &b )
        {
           b.f();
        }
        
        int main()
        {
           D1 d1;
           g( d1 );
        
           D2 d2;
           g( d2 );
        }
        

        第二种情况

        #include <iostream>
        
        struct B
        {
        };
        
        
        struct D1
        {
           void f() const { std::cout << "It's me, D1!" << std::endl; }
        };
        
        
        struct D2
        {
           void f() const { std::cout << "No, it's me, D2!" << std::endl; }
        };
        
        
        void g( const B &b )
        {
           // b.f(); // compilation error
        }
        
        
        void g1( const D1 &d1 )
        {
           d1.f();
        }
        
        void g2( const D2 &d2 )
        {
           d2.f();
        }
        
        int main()
        {
           D1 d1;
           g1( d1 );
           g( d1 ); // function f will not be called
           //g2( d1 ); // compilation error
        
           D2 d2;
           g2( d2 );
           g( d2 ); // function f will not be called
           //g1( d2 ); // compilation error
        }
        

        【讨论】:

          猜你喜欢
          • 2017-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-11
          • 2021-11-09
          • 2014-04-04
          • 2015-02-01
          相关资源
          最近更新 更多