zoukankan      html  css  js  c++  java
  • C++11 之 override

    override 关键字

    作用:在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数。
    位置:函数调用运算符之后,函数体或纯虚函数标识 “= 0” 之前。

    使用以后有以下好处:
    1.可以当注释用,方便阅读.
    2.告诉阅读你代码的人,这是方法的复写.
    3.编译器可以给你验证 override 对应的方法名是否是你父类中所有的,如果没有则报错.

    override 使用举例

    如果你想重写父类的方法,比如toString()方法:

    正确的是:

    public String toString() override {
    	...
    }
    

    假如不小心把方法名写错了而没写 override ,这时编译器是可以编译通过的,因为编译器以为这个方法是你的子类中自己增加的方法。如:

    // 注意这里的小写方法,实际上是错误的。
    public String tostring() {...}
    
    

    相反,如果你很机智,在知道自己要重写父类的方法,加上了 override 标签后,编译器会检查出重写方法错误,会保证你重写父类方法的正确性。

    #include <memory>
    #include <iostream>
    using namespace std; 
    class Base
    {
    public:
                virtual void foo();
    };
    void Base::foo()
    {
            cout << "Base::foo" <<endl;
    }
     
    class Derived : public  Base
    {
                void foo() override; // OK: Derived::foo overrides Base::foo
    };
    void Derived::foo()
    {
            cout << "Derived::foo" <<endl;
    }
    int main()
    {
            Base * b = new Derived();
            b->foo();
            return 0;
    } 
    root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
    root@ubuntu:~/c++# ./over
    Derived::foo

    非虚函数

    #include <memory>
    #include <iostream>
    using namespace std;
    class Base
    {
    public:
                //virtual void foo();
                void foo();
    };
    void Base::foo()
    {
            cout << "Base::foo" <<endl;
    }
    
    class Derived : public  Base
    {
                void foo() override; // OK: Derived::foo overrides Base::foo
    };
    void Derived::foo()
    {
            cout << "Derived::foo" <<endl;
    }
    int main()
    {
            Base * b = new Derived();
            b->foo();
            return 0;
    }
    over.cpp:17:11: error: ‘void Derived::foo()’ marked ‘override’, but does not override
          void foo() override; // OK: Derived::foo overrides Base::foo
               ^
    #include <memory>
    #include <iostream>
    using namespace std; 
    class Base
    {
    public:
                virtual void foo();
    };
    void Base::foo()
    {
            cout << "Base::foo" <<endl;
    }
     
    class Derived : public  Base
    {
                void foo(int a) override; // OK: Derived::foo overrides Base::foo
    };
    void Derived::foo(int a)
    {
            cout << "Derived::foo" <<endl;
    }
    int main()
    {
            Base * b = new Derived();
            b->foo(3);
            return 0;
    } 
    root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
    over.cpp:16:11: error: ‘void Derived::foo(int)’ marked ‘override’, but does not override
          void foo(int a) override; // OK: Derived::foo overrides Base::foo
               ^
    over.cpp: In function ‘int main()’:
    over.cpp:25:10: error: no matching function for call to ‘Base::foo(int)’
      b->foo(3);
              ^
    over.cpp:9:6: note: candidate: virtual void Base::foo()
     void Base::foo()
          ^
    over.cpp:9:6: note:   candidate expects 0 arguments, 1 provided
    #include <memory>
    #include <iostream>
    using namespace std;
    class Base
    {
    public:
                virtual void foo();
    };
    void Base::foo()
    {
            cout << "Base::foo" <<endl;
    }
    
    class Derived : public  Base
    {
                void foo() ; // OK: Derived::foo overrides Base::foo
                //void foo() override; // OK: Derived::foo overrides Base::foo
    };
    //void Derived::foo(int a)
    //{
    //      cout << "Derived::foo" <<endl;
    //}
    int main()
    {
            Base * b = new Derived();
            b->foo();
            return 0;
    }
    root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
    /tmp/cc9ksedx.o: In function `Derived::Derived()':
    over.cpp:(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x14): undefined reference to `vtable for Derived'
    over.cpp:(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x18): undefined reference to `vtable for Derived'
    collect2: error: ld returned 1 exit status
    #include <memory>
    #include <iostream>
    using namespace std;
    class Base
    {
    public:
                virtual void foo();
    };
    void Base::foo()
    {
            cout << "Base::foo" <<endl;
    }
    
    class Derived : public  Base
    {
               void Foo() override; // OK: Derived::foo overrides Base::foo
    };
    //void Derived::foo(int a)
    //{
    //      cout << "Derived::foo" <<endl;
    //}
    int main()
    {
            Base * b = new Derived();
            //b->foo();
            return 0;
    }
    root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
    over.cpp:16:10: error: ‘void Derived::Foo()’ marked ‘override’, but does not override
         void Foo() override; // OK: Derived::foo overrides Base::foo
              ^
    root@ubuntu:~/c++#
  • 相关阅读:
    MySQL批量更新字段url链接中的域名
    巧用Win+R
    斯坦福高效睡眠法
    chkconfig: command not found
    Nginx(./configure --help)
    Ubuntu16.04配置Tomcat的80端口访问
    Binary Tree Level Order Traversal
    java——Arrays.asList()方法
    python 发送邮件
    常用邮件协议
  • 原文地址:https://www.cnblogs.com/dream397/p/14607821.html
Copyright © 2011-2022 走看看